Main Content

Handle Sparse Arrays in C MEX File

The C Matrix API provides a set of functions that allow you to create and manipulate sparse arrays from within your MEX files. These API routines access and manipulate ir and jc, two of the parameters associated with sparse arrays. For more information on how MATLAB® stores sparse arrays, see The MATLAB Array.

The example fulltosparseIC.c shows how to populate a sparse matrix.

Build the example.

mex -R2018a fulltosparseIC.c

Create a full, 5-by-5 identity matrix.

full = eye(5)
full =
     1     0     0     0     0
     0     1     0     0     0
     0     0     1     0     0
     0     0     0     1     0
     0     0     0     0     1

Call fulltosparseIC to produce the corresponding sparse matrix.

spar = fulltosparseIC(full)
spar =
   (1,1)        1
   (2,2)        1
   (3,3)        1
   (4,4)        1
   (5,5)        1

Related Topics