Matlab crashed when call mexCallMATLAB
Mostrar comentarios más antiguos
I want to create a new sparse matrix and pass the value of the input sparse matrix to it, and then call mexCallMATLAB to get its transpose. The code crashed,
#include <stdio.h>
#include <stdlib.h>
#include <mex.h>
#include <math.h>
#include <time.h>
#include <string.h>
extern void mexFunction(int iNbOut, mxArray *pmxOut[],
int iNbIn, const mxArray *pmxIn[])
{
mxArray *A, *B;
double *d,*p,*pin,*out;
mwSize m,n,nzmax;
mwIndex *ir, *jc;
m = mxGetM(pmxIn[0]);
n = mxGetN(pmxIn[0]);
nzmax = mxGetNzmax(pmxIn[0]);
ir = mxGetIr(pmxIn[0]);
jc = mxGetJc(pmxIn[0]);
pin = mxGetPr(pmxIn[0]);
A = mxCreateSparse(m, n, nzmax, mxREAL);
mxSetIr(A, ir);
mxSetJc(A, jc);
mxSetPr(A, pin);
d = mxGetPr(A);
B = mxCreateSparse(m, n, nzmax, mxREAL);
mexCallMATLAB(1, &B, 1, &A, "transpose");
p = mxGetPr(B);
pmxOut[0] = mxCreateNumericArray(1 , nzmax, mxSINGLE_CLASS,mxREAL);
out = mxGetPr(pmxOut[0]);
for(mwSize i = 0; i < nzmax; i++)
{
out[i] = p[i];
mexPrintf("pmxOut = %f", out[i]);
}
}
Respuesta aceptada
Más respuestas (2)
bo bo
el 7 de Mzo. de 2017
0 votos
1 comentario
James Tursa
el 7 de Mzo. de 2017
Editada: James Tursa
el 7 de Mzo. de 2017
Can you write out, in pseudo-code or descriptions, line by line, what it is you want to do?
The mxCreateSparse function simply gives a bare bones sparse matrix. To get a specific structure you have to manually assign values to the Ir, Jc, Pr, and Pi (if applicable) parts. So if you are trying to duplicate the sparse structure of an existing sparse matrix, you would need to copy over all of the Ir and Jc stuff (as opposed to merely copying the pointers over as you tried to do). A simple way to accomplish all this is to use mxDuplicateArray (but that will copy over all of the Pr and Pi values as well, which may do more work than you want).
If d is a pointer to an existing valid mxArray, you can get its transpose by simply
mxArray *dt;
:
mexCallMATLAB(1, &dt, 1, &d, "transpose");
bo bo
el 7 de Mzo. de 2017
2 comentarios
James Tursa
el 7 de Mzo. de 2017
Editada: James Tursa
el 7 de Mzo. de 2017
Typo on my part. Use mxCreateNumericMatrix, not mxCreateNumericArray.
Also, I suppose I should point out that nzmax is simply the max amount of non-zeros that can be stored using currently allocated memory. It does not represent how many non-zeros the sparse matrix actually has. To get that number you would do this instead:
mwIndex *jc;
size_t n, nnz;
:
jc = mxGetJc(pmxIn[0]);
n = mxGetN(pmxIn[0]);
nnz = jc[n]; /* The number of non-zero entries in the sparse matrix */
bo bo
el 9 de Mzo. de 2017
Categorías
Más información sobre Write C Functions Callable from MATLAB (MEX Files) en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!