manpagez: man pages & more
info octave
Home | html | info | man
[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

A.2.6 Sparse Matrices with Mex-Files

The Octave format for sparse matrices is identical to the mex format in that it is a compressed column sparse format. Also in both, sparse matrices are required to be two-dimensional. The only difference is that the real and imaginary parts of the matrix are stored separately.

The mex-file interface, as well as using mxGetM, mxGetN, mxSetM, mxSetN, mxGetPr, mxGetPi, mxSetPr and mxSetPi, the mex-file interface supplies the functions

mwIndex *mxGetIr (const mxArray *ptr);
mwIndex *mxGetJc (const mxArray *ptr);
mwSize mxGetNzmax (const mxArray *ptr);

void mxSetIr (mxArray *ptr, mwIndex *ir);
void mxSetJc (mxArray *ptr, mwIndex *jc);
void mxSetNzmax (mxArray *ptr, mwSize nzmax);

mxGetNzmax gets the maximum number of elements that can be stored in the sparse matrix. This is not necessarily the number of non-zero elements in the sparse matrix. mxGetJc returns an array with one additional value than the number of columns in the sparse matrix. The difference between consecutive values of the array returned by mxGetJc define the number of non-zero elements in each column of the sparse matrix. Therefore

mwSize nz, n;
mwIndex *Jc;
mxArray *m;
n = mxGetN (m);
Jc = mxGetJc (m);
nz = Jc[n];

returns the actual number of non-zero elements stored in the matrix in nz. As the arrays returned by mxGetPr and mxGetPi only contain the non-zero values of the matrix, we also need a pointer to the rows of the non-zero elements, and this is given by mxGetIr. A complete example of the use of sparse matrices in mex-files is given by the file ‘mysparse.c’ as seen below

© 2000-2018
Individual documents may contain additional copyright information.