Creating sparse matrix in MEX

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

 采纳的回答

James Tursa
James Tursa 2015-10-16
编辑:James Tursa 2015-10-16
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).

更多回答(0 个)

类别

在 帮助中心 和 File Exchange 中查找有关 Sparse Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by