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 Center および File ExchangeSparse Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by