User`s manual

Examples of C MEX-Files
2-31
}
/* Check data type of input argument. */
if(!(mxIsDouble(prhs[0]))) {
mexErrMsgTxt("Input argument must be of type double.");
}
if(mxGetNumberOfDimensions(prhs[0]) != 2) {
mexErrMsgTxt("Input argument must be two dimensional\n");
}
/* Get the size and pointers to input data. */
m = mxGetM(prhs[0]);
n = mxGetN(prhs[0]);
pr = mxGetPr(prhs[0]);
pi = mxGetPi(prhs[0]);
cmplx = (pi == NULL ? 0 : 1);
/* Allocate space for sparse matrix.
* NOTE: Assume at most 20% of the data is sparse. Use ceil
* to cause it to round up.
*/
percent_sparse = 0.2;
nzmax = (int)ceil((double)m*(double)n*percent_sparse);
plhs[0] = mxCreateSparse(m,n,nzmax,cmplx);
sr = mxGetPr(plhs[0]);
si = mxGetPi(plhs[0]);
irs = mxGetIr(plhs[0]);
jcs = mxGetJc(plhs[0]);
/* Copy nonzeros. */
k = 0;
isfull = 0;
for(j = 0; (j < n ); j++) {
int i;
jcs[j] = k;
for(i = 0; (i < m ); i++) {