Using the C Math Library    

Sparse Matrices

The MATLAB C Math Library includes routines to create and manipulate sparse arrays. Sparse matrices provides a more efficient storage format for two-dimensional numeric arrays with few non-zero elements. Only two-dimensional numeric arrays can be converted to sparse storage format.

This table lists the MATLAB C Math Library routines used to create sparse matrices and perform some basic operations on them. The sections that follow provide more detail about using these routines. For more detailed information about using sparse arrays, see Using MATLAB. For more detailed information about any of the library routines, see the online MATLAB C Math Library Reference.

Table 3-2: Sparse Matrix Routines 
To ...
Use ...
Create a sparse matrix
mlfSparse()
Convert a sparse matrix into a full matrix
mlfFull()
Replace nonzero sparse matrix elements with ones
mlfSpones()

Replace nonzero sparse matrix elements with random numbers
mlfSprand()
mlfSprandn()
mlfSprandnsym()
Import from external sparse matrix format
mlfSpconvert()
Create a sparse identity matrix
mlfSpeye()
Extract a band or diagonal group of elements from a matrix and create a sparse matrix
mlfSpdiags()
Determine the number of nonzero elements in a numeric matrix.
mlfNnz()
Determine if a matrix has any nonzero elements or if all elements are nonzero
mlfAny() or
mlfAll()
Determine the amount of storage allocated for the nonzero elements of a sparse matrix
mlfNzmax()
Apply a function to all the nonzero elements of a sparse matrix
mlfSpfun()


 Example Program: Creating Numeric Arrays (ex1.c) Creating a Sparse Matrix