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.
Type: double | complex
Dimension: matrix
row
The row indices of the non-zero elements.
Type: integer
Dimension: vector
col
The column indices of the non-zero elements.
Type: integer
Dimension: vector
val
The non-zero element values.
Type: double | complex
Dimension: vector
m
The number of rows in the matrix.
Default: max(row).
Type: integer
Dimension: scalar
n
The number of columns in the matrix.
Default: max(col).
Type: integer
Dimension: scalar

Outputs

s
The sparse matrix.
Type: double | complex
Dimension: 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.