|On this page…|
It is not uncommon to have matrices with a large number of zero-valued elements and, because the MATLAB® software stores zeros in the same way it stores any other numeric value, these elements can use memory space unnecessarily and can sometimes require extra computing time.
Sparse matrices provide a way to store data that has a large percentage of zero elements more efficiently. While full matrices internally store every element in memory regardless of value, sparse matrices store only the nonzero elements and their row indices. Using sparse matrices can significantly reduce the amount of memory required for data storage.
You can create sparse matrices for the double and logical classes. All MATLAB built-in arithmetic, logical, and indexing operations can be applied to sparse matrices, or to mixtures of sparse and full matrices. Operations on sparse matrices return sparse matrices and operations on full matrices return full matrices.
This table shows some of the functions most commonly used when working with sparse matrices.
Convert a sparse matrix to a full matrix.
Determine if a matrix is sparse.
Return the number of nonzero matrix elements.
Return the nonzero elements of a matrix.
Return the amount of storage allocated for nonzero elements.
Allocate space for a sparse matrix.
Create a sparse matrix or convert full to sparse.
Create a sparse identity matrix.
Create a sparse uniformly distributed random matrix.