array als rückgabe einer funktion (mex)

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • array als rückgabe einer funktion (mex)

    hallo erstmal :)

    das problem gabs zwar schonmal (easy-coding.de/array-und-funktionen-t1877.html), aber leider versteh ich die antwort nicht so recht :roll: da ich im bezug auf arrays absolut gar keine ahnung habe :) im speziellen, wie diese in c++ initiiert und definiert werden...

    daher hier mein spezifisches problem:
    ich muss eine matrix mit einem vektor multiplizieren. das ganze soll so ablaufen, dass die hauptprozedur eine funktion aufruft, der die matrix und der vektor übergeben werden. die funktion berechnet dann das matrix-vektor-produkt.

    hier mal der code:

    Quellcode

    1. #include "mex.h"
    2. double mult(int nor, int noc, double matA[][], double vecx[])
    3. {
    4. int i = 0;
    5. int j = 0;
    6. double res[nor];
    7. for (i = 0; i < nor; i++)
    8. {
    9. res[i] = 0.0;
    10. }
    11. for (i = 0; i < nor; i++)
    12. {
    13. for (j = 0; j < noc; j++)
    14. {
    15. res[i] = res[i] + (matA[i][j] * vecx[j]);
    16. }
    17. }
    18. return res;
    19. }
    20. void mexFunction(int nlhs, mxArray *plhs[ ],int nrhs, const mxArray *prhs[ ])
    21. {
    22. /* check number of input arguments */
    23. if (nrhs != 2)
    24. {
    25. mexErrMsgTxt("Must have two input arguments.");
    26. }
    27. /* check number of output arguments */
    28. if (nlhs != 1)
    29. {
    30. mexErrMsgTxt("Must have one output argument.");
    31. }
    32. /* check if second input argument is a vector. If yes check if it is a column vector, else GGF. PROBLEM! */
    33. int vecCheckN = mxGetN(prhs[1]); /* number of columns of the input vector */
    34. int vecCheckM = mxGetM(prhs[1]); /* number of rows of the input vector */
    35. if (vecCheckN != 1)
    36. {
    37. if (vecCheckM != 1)
    38. {
    39. mexErrMsgTxt("Second input argument must be a vector.");
    40. }
    41. mexErrMsgTxt("Second input argument must be a column vector.");
    42. // ZEILENVEKTOR EINGEGEBEN! GGF. PROBLEME BEACHTEN!
    43. // vecCheckM = vecCheckN;
    44. }
    45. /* check if matrix and vector dimensions agree */
    46. int n = mxGetN(prhs[0]); /* number of columns of the input matrix */
    47. if (n != vecCheckN)
    48. {
    49. mexErrMsgTxt("Matrix and vector dimensions disagree.");
    50. }
    51. /* declare variables */
    52. int checkSparse = 0;
    53. int m = mxGetM(prhs[0]); /* number of rows of the input matrix */
    54. double matrixA[m][n];
    55. double vectorx[n];
    56. double result[m]
    57. // SCHLEIFE ZUM BEFÜLLEN VON matrixA UND vectorx EINFÜGEN
    58. /* check if matrix is a sparse matrix */
    59. if ( mxIsClass(prhs[0],"sparse"))
    60. {
    61. checkSparse = 1;
    62. }
    63. /* choose way to compute result and compute*/
    64. switch (checkSparse)
    65. {
    66. case 0:
    67. result = mult(m,n,matrixA,vectorx);
    68. break;
    69. case 1: // IS SPARSE: BERECHNUNG PER mexCallMATLAB
    70. break;
    71. default:
    72. break;
    73. }
    74. }
    75. }
    Alles anzeigen


    ok, wie ihr seht, geht es um mex-programmierung :)
    mein problem ist die definition der funktion mult, sowie ihrer übergabewerte, usw. das programm müsste möglichst "schnell" sein, sprich es geht nicht um schöne formulierung, sondern um möglichst gute performance.

    kurze zusammenfassung vom code:
    in zeile 78 wird die funktion mult aufgerufen, die ein produkt von einem 2-d array und einem 1-d array berechnet (was wiederrum ein 1-d array ist)
    und dieser ergebnissarray soll dann halt in result gespeichert werden.

    hoffe ihr könnt mir helfen :) bin noch c/c++ neuling und fang grad erst an :) also bitte nich haun ;)