I'm not sure what your real question is. Do you just want to print a sparse matrix from within a mex function? If so, you can use this function:
void spprint(const mxArray *mx)
{
mwSize n, nrow;
mwIndex *ir, *jc;
mwIndex j, x, y;
double *pr;
if( !mxIsSparse(mx) ) return;
n = mxGetN(mx);
pr = mxGetPr(mx);
ir = mxGetIr(mx);
jc = mxGetJc(mx);
for( y=0; y<n; y++ ) {
nrow = jc[y+1] - jc[y];
for( x=0; x<nrow; x++ ) {
mexPrintf(" (%d,%d) %g\n",(*ir++)+1,y+1,*pr++);
}
}
}
Incidentally, you should never recast function pointer return values like this:
jc = (int*) mxGetJc(prhs[0]);
ir = (int*) mxGetIr(prhs[0]);
This will not work if mwIndex is not an int. Best to just declare jc and ir as mwIndex pointers (not int pointers) and then don't cast the result of the functions and you will be fine.
Best Answer