Creating sparse matrix in MEX

2 views (last 30 days)
ABDO
ABDO on 16 Oct 2015
Edited: James Tursa on 16 Oct 2015
Hello
please i have sparse matrix A(M,N) in mex programme declared in this form
double **A;
A=(double **) mxCalloc( M, sizeof(double));
for( i = 0; i < M; i++) {
*(A + i) = (double *) mxCalloc( N, sizeof(double));
}
i would like to declare it sparse using
mxCreateSparse(m,n,nz,mxREAL);
please how can i do this?
Thanks
Abdelilah

Accepted Answer

James Tursa
James Tursa on 16 Oct 2015
Edited: James Tursa on 16 Oct 2015
First, this line is incorrect:
A=(double **) mxCalloc( M, sizeof(double));
It should be one of the following instead:
A=(double **) mxCalloc( M, sizeof(*A)); // I prefer this one
or
A=(double **) mxCalloc( M, sizeof(double *));
But the answer to your real question will depend on what you are doing with the sparse matrix downstream in your code. Your "full" code creates a potentially non-contiguous matrix using an "array" of pointers. All of the double values are 0. The "sparse" code you show does the equivalent ... it creates a matrix of the same size where all the double values are 0 (assuming m==M and n==N and nz=something_reasonable). So you already have code that creates the sparse version of the full 0's matrix, as long as you assign it to a variable. E.g.,
mxArray *ms;
ms = mxCreateSparse(m,n,nz,mxREAL);
My guess is you need to fill in the non-zero values of the sparse matrix? If so, please provide more detail about what you are doing and where these non-zero values are located within the sparse matrix and then we can discuss how to code that (e.g., modifying the Pr and Ir and Jc data).

More Answers (0)

Categories

Find more on External Language Interfaces in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!