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:
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
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
- #include "mex.h"
- double mult(int nor, int noc, double matA[][], double vecx[])
- {
- int i = 0;
- int j = 0;
- double res[nor];
- for (i = 0; i < nor; i++)
- {
- res[i] = 0.0;
- }
- for (i = 0; i < nor; i++)
- {
- for (j = 0; j < noc; j++)
- {
- res[i] = res[i] + (matA[i][j] * vecx[j]);
- }
- }
- return res;
- }
- void mexFunction(int nlhs, mxArray *plhs[ ],int nrhs, const mxArray *prhs[ ])
- {
- /* check number of input arguments */
- if (nrhs != 2)
- {
- mexErrMsgTxt("Must have two input arguments.");
- }
- /* check number of output arguments */
- if (nlhs != 1)
- {
- mexErrMsgTxt("Must have one output argument.");
- }
- /* check if second input argument is a vector. If yes check if it is a column vector, else GGF. PROBLEM! */
- int vecCheckN = mxGetN(prhs[1]); /* number of columns of the input vector */
- int vecCheckM = mxGetM(prhs[1]); /* number of rows of the input vector */
- if (vecCheckN != 1)
- {
- if (vecCheckM != 1)
- {
- mexErrMsgTxt("Second input argument must be a vector.");
- }
- mexErrMsgTxt("Second input argument must be a column vector.");
- // ZEILENVEKTOR EINGEGEBEN! GGF. PROBLEME BEACHTEN!
- // vecCheckM = vecCheckN;
- }
- /* check if matrix and vector dimensions agree */
- int n = mxGetN(prhs[0]); /* number of columns of the input matrix */
- if (n != vecCheckN)
- {
- mexErrMsgTxt("Matrix and vector dimensions disagree.");
- }
- /* declare variables */
- int checkSparse = 0;
- int m = mxGetM(prhs[0]); /* number of rows of the input matrix */
- double matrixA[m][n];
- double vectorx[n];
- double result[m]
- // SCHLEIFE ZUM BEFÜLLEN VON matrixA UND vectorx EINFÜGEN
- /* check if matrix is a sparse matrix */
- if ( mxIsClass(prhs[0],"sparse"))
- {
- checkSparse = 1;
- }
- /* choose way to compute result and compute*/
- switch (checkSparse)
- {
- case 0:
- result = mult(m,n,matrixA,vectorx);
- break;
- case 1: // IS SPARSE: BERECHNUNG PER mexCallMATLAB
- break;
- default:
- break;
- }
- }
- }
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