Mathematics |
Introduction
Sparse matrices are a special class of matrices that contain a significant number of zero-valued elements. This property allows MATLAB to:
This section provides information about:
Sparse Matrix Storage
For full matrices, MATLAB stores internally every matrix element. Zero-valued elements require the same amount of storage space as any other matrix element. For sparse matrices, however, MATLAB stores only the nonzero elements and their indices. For large matrices with a high percentage of zero-valued elements, this scheme significantly reduces the amount of memory required for data storage.
MATLAB uses three arrays internally to store sparse matrices with real elements. Consider an m
-by-n
sparse matrix with nnz
nonzero entries stored in arrays of length nzmax
:
nzmax
.
nnz
entries. This array also has length equal to nzmax
.
n
integer pointers to the start of each column in the other arrays and an additional pointer that marks the end of those arrays. The length of the third array is n+1
.
This matrix requires storage for nzmax
floating-point numbers and nzmax+n+1
integers. At 8 bytes per floating-point number and 4 bytes per integer, the total number of bytes required to store a sparse matrix is
Sparse matrices with complex elements are also possible. In this case, MATLAB uses a fourth array with nnz
elements to store the imaginary parts of the nonzero elements. An element is considered nonzero if either its real or imaginary part is nonzero.
General Storage Information
The whos
command provides high-level information about matrix storage, including size and storage class. For example, this
whos
listing shows information about sparse and full versions of the same matrix.
whos Name Size Bytes Class M_full 1100x1100 9680000 double array M_sparse 1100x1100 4404 sparse array Grand total is 1210000 elements using 9684404 bytes
Notice that the number of bytes used is much less in the sparse case, because zero-valued elements are not stored. In this case, the density of the sparse matrix is 4404/9680000, or approximately .00045%.
Function Summary | Creating Sparse Matrices |