Sparse Matrices

Sparse matrices are a special case of 2D matrices, where only non-zero elements are stored and displayed for efficiency. A sparse matrix typically has a high percentage of zero elements.

All rules that apply to accessing elements of a 2D matrix also apply to sparse matrices.

For example, running this script:
s = sparse([1,2,3,4,3],[4,2,3,1,2],[30,40,50,60,70])
col2 = s(:,2)
s(3,:) = [10,0,0,80]
produces this output, showing the row, column, and value for each non-zero element in the same order as they are stored in memory:
s = sparse [4 x 4], nnz = 5
[4,1] 60
[2,2] 40
[3,2] 70
[3,3] 50
[1,4] 30

col2 = sparse [4 x 1], nnz = 2
[2,1] 40
[3,1] 70

s = sparse [4 x 4], nnz = 5
[3,1] 10
[4,1] 60
[2,2] 40
[1,4] 30
[3,4] 80
Note: The output of a slice operation is also a sparse matrix.

Comments

Support for sparse matrices in functions and arithmetic operators is currently only partial. Please consult the specific documentation to see the available support for arithmetic operators.