Handle Sparse Arrays

The MATLAB® 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, fulltosparse.c, illustrates how to populate a sparse matrix. To see the example, open the fileopen the file in MATLAB Editor.

Build the example.

mex fulltosparse.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

Callfulltosparse to produce the corresponding sparse matrix.

spar = fulltosparse(full)
spar =
   (1,1)        1
   (2,2)        1
   (3,3)        1
   (4,4)        1
   (5,5)        1
Was this topic helpful?