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.

+3


source to share


1 answer


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

    (and pi

    if the matrix is ​​complex) is a double-precision array of length nzmax

    containing non-zero values ​​of the matrix.

  • ir

    points to an integer array, also of length nzmax

    , containing the string indices of the corresponding elements in pr

    and pi

    .

  • jc

    points to an integer array of length n+1

    , where n

    is the number of columns in the sparse matrix. The array jc

    contains the index information of the column. If the j

    th column of the sparse matrix has any nonzero entries, jc[j]

    is the index in ir

    and pr

    (and pi

    if it exists) of the first nonzero element in the j

    -th column, and jc[j+1] - 1

    is the index of the last nonzero element in that column. For the j

    th column of the sparse matrix jc[j]

    , the total number of nonzero elements in all previous columns. The last element of the array jc

    , jc[n]

    is equal to nnz

    , the number of nonzero elements in all sparse matrix.

+2


source







All Articles