Navigation

Operators and Keywords

Function List:

C++ API

: s = sparse (a)
: s = sparse (i, j, sv, m, n)
: s = sparse (i, j, sv)
: s = sparse (m, n)
: s = sparse (i, j, s, m, n, "unique")
: s = sparse (i, j, sv, m, n, nzmax)

Create a sparse matrix from a full matrix, or row, column, value triplets.

If a is a full matrix, convert it to a sparse matrix representation, removing all zero values in the process.

Given the integer index vectors i and j, and a 1-by-nnz vector of real or complex values sv, construct the sparse matrix S(i(k),j(k)) = sv(k) with overall dimensions m and n. If any of sv, i or j are scalars, they are expanded to have a common size.

If m or n are not specified their values are derived from the maximum index in the vectors i and j as given by m = max (i), n = max (j).

Note: if multiple values are specified with the same i, j indices, the corresponding value in s will be the sum of the values at the repeated location. See accumarray for an example of how to produce different behavior, such as taking the minimum instead.

If the option "unique" is given, and more than one value is specified at the same i, j indices, then the last specified value will be used.

sparse (m, n) will create an empty mxn sparse matrix and is equivalent to sparse ([], [], [], m, n)

The argument nzmax is ignored but accepted for compatibility with MATLAB.

Example 1 (sum at repeated indices):

i = [1 1 2]; j = [1 1 2]; sv = [3 4 5];
sparse (i, j, sv, 3, 4)
⇒
Compressed Column Sparse (rows = 3, cols = 4, nnz = 2 [17%])

 (1, 1) ->  7
 (2, 2) ->  5

Example 2 ("unique" option):

i = [1 1 2]; j = [1 1 2]; sv = [3 4 5];
sparse (i, j, sv, 3, 4, "unique")
⇒
Compressed Column Sparse (rows = 3, cols = 4, nnz = 2 [17%])

 (1, 1) ->  4
 (2, 2) ->  5

See also: full, accumarray, spalloc, spdiags, speye, spones, sprand, sprandn, sprandsym, spconvert, spfun.

Package: octave