spconvert

Converts a list of (row, column, value) entries to a sparse matrix.

Category: Mathematics > Sparse Matrices

Syntax

S = spconvert(D)

Builds a sparse matrix from the rows [i j v] of D.

Description

spconvert reads the format commonly used to save sparse matrices as text, one entry per line. Loading such a file with load gives the matrix D, and spconvert(D) turns it into a sparse matrix.

The size is taken from the largest indices that appear, so adding a row with value 0 at the bottom-right position is how a matrix with empty trailing rows or columns is specified.

Input arguments

D
Matrix with three columns [i j v], or four columns [i j re im] for complex entries. An entry with value 0 at the last position sets the size.

Output arguments

S
Sparse matrix whose size is the largest row and column index in D.

Example

Triplets with a size marker

D = [1 1 5; 2 3 7; 4 4 0];
S = spconvert(D);
size(S)
full(S)
Output
ans = 1×2

     4     4
ans = 4×4

     5     0     0     0
     0     0     7     0
     0     0     0     0
     0     0     0     0

See also

sparse find load