sparse
Sparse matrix creation.
Syntax
s = sparse(a)
s = sparse(row, col, v)
s = sparse(m, n)
s = sparse(row, col, val, m, n)
s = sparse(row, col, val, m, n, 'unique')
Inputs
- a
- A fully populated matrix to be converted to the sparse format.
- row
- The row indices of the non-zero elements.
- col
- The column indices of the non-zero elements.
- val
- The non-zero element values.
- m
- The number of rows in the matrix.
- n
- The number of columns in the matrix.
Outputs
- s
- The sparse matrix.
Example
row = [1, 2, 4, 4, 1, 5, 2, 3, 4, 1, 2, 3, 4, 1, 4];
col = [1, 1, 1, 2, 3, 3, 4, 4, 4, 5, 5, 5, 5, 6, 6];
vals = [10, 20, 40, -90, 110, 150, 170, 180, 190, 210, -220, 230, 240, 260, 290];
s = sparse(row, col, vals)
s = sparse [5 x 6], nnz = 15
[1,1] 10
[2,1] 20
[4,1] 40
[4,2] -90
[1,3] 110
[5,3] 150
[2,4] 170
[3,4] 180
[4,4] 190
[1,5] 210
[2,5] -220
[3,5] 230
[4,5] 240
[1,6] 260
[4,6] 290
Comments
Sparse matrices are stored in the compressed column storage format.
When using row, col, the m, n inputs can be defaulted with []. If there are repeated element positions, their values will be summed unless the 'unique' flag is used, in which case only the last value will be used.