Documentation |
On this page… |
---|
Using sparse matrices to store data that contains a large number of zero-valued elements can both save a significant amount of memory and speed up the processing of that data. sparse is an attribute that you can assign to any two-dimensional MATLAB^{®} matrix that is composed of double or logical elements.
The sparse attribute allows MATLAB to:
Store only the nonzero elements of the matrix, together with their indices.
Reduce computation time by eliminating operations on zero elements.
For full matrices, MATLAB stores every matrix element internally. 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.
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.
M_full = magic(1100); % Create 1100-by-1100 matrix. M_full(M_full > 50) = 0; % Set elements >50 to zero. M_sparse = sparse(M_full); % Create sparse matrix of same. whos Name Size Bytes Class Attributes M_full 1100x1100 9680000 double M_sparse 1100x1100 5004 double sparse
Notice that the number of bytes used is fewer in the sparse case, because zero-valued elements are not stored.
Sparse matrices also have significant advantages in terms of computational efficiency. Unlike operations with full matrices, operations with sparse matrices do not perform unnecessary low-level arithmetic, such as zero-adds (x+0 is always x). The resulting efficiencies can lead to dramatic improvements in execution time for programs working with large amounts of sparse data.
For more information, see Sparse Matrix Operations.