Creating sparse matrix in MEX

조회 수: 3 (최근 30일)
ABDO
ABDO 2015년 10월 16일
편집: James Tursa 2015년 10월 16일
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개)

카테고리

Help CenterFile Exchange에서 Sparse Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by