Using the C Math Library    

Creating a Sparse Matrix

To create a sparse matrix, call the MATLAB C Math Library mlfSparse() routine. Using this routine, you can create sparse arrays in two ways:

Converting an Existing Matrix into Sparse Format

To create a sparse matrix from a standard numeric array, use the mlfSparse() routine. mlfSparse() converts the numeric array into sparse storage format.

To illustrate, the following code fragment creates a 12-by-12 identity matrix. Of the 144 elements in this matrix, only 12 elements have nonzero values. In full format, all 144 are allocated storage. When this identity matrix is converted to sparse matrix format, only the 12 nonzero elements have storage allocated for them.

In the example, the NULLs included in the call to mlfSparse() represent optional arguments. The following section describes these optional arguments.

This code displays the identity matrix in full and sparse formats.

Creating a Sparse Matrix from Data

You can create a sparse matrix, specifying the value and location of all the nonzero elements when you create it. Using mlfSparse(), you specify as arguments:

The following code example illustrates how to create a sparse 8-by-7 sparse matrix from data. This call specifies a single value, 9, for all the nonzero elements of the sparse matrix which is replicated in all nonzero elements by scalar expansion. To see the pattern formed by this sparse matrix, see the output of this code which follows.

This code produces the following output.


 Sparse Matrices Converting a Sparse Matrix to Full Matrix Format