How to pass a sparse matrix to a shared library from MATLAB
I want to pass a sparse matrix to a shared library from MATLAB, perform some operation there (written in C), and then return it.
I can pass a dense matrix and use it quite simply. But I don't know how to pass the sparse matrix to the shared library from MATLAB. What I found is everyone is concerned about MEX.
It's clear if you give some information about the sparse matrix format in MATLAB and the conversion to C.
Thanks in advance.
source to share
Internally, MATLAB stores sparse matrices using the Compressed Sparse Column Format (CSC). Once you understand the format, you can pass a sparse matrix to the external code received arrays pr
, pi
, ir
and jc
(using the MEX-function mxGetPr , mxGetPi , mxGetIr , mxGetJc respectively.)
-
pr
(andpi
if the matrix is complex) is a double-precision array of lengthnzmax
containing non-zero values of the matrix. -
ir
points to an integer array, also of lengthnzmax
, containing the string indices of the corresponding elements inpr
andpi
. -
jc
points to an integer array of lengthn+1
, wheren
is the number of columns in the sparse matrix. The arrayjc
contains the index information of the column. If thej
th column of the sparse matrix has any nonzero entries,jc[j]
is the index inir
andpr
(andpi
if it exists) of the first nonzero element in thej
-th column, andjc[j+1] - 1
is the index of the last nonzero element in that column. For thej
th column of the sparse matrixjc[j]
, the total number of nonzero elements in all previous columns. The last element of the arrayjc
,jc[n]
is equal tonnz
, the number of nonzero elements in all sparse matrix.
source to share