S = sparse(i,j,v) generates
a sparse matrix S from the triplets i, j,
and v such that S(i(k),j(k)) = v(k).
The max(i)-by-max(j) output
matrix has space allotted for length(v) nonzero
elements. sparse adds together elements in v that
have duplicate subscripts in i and j.

If the inputs i, j, and v are
vectors or matrices, they must have the same number of elements. Alternatively,
the argument v and/or one of the arguments i or j can
be scalars.

Subscript pairs, specified as separate arguments of scalars,
vectors, or matrices. Corresponding elements in i and j specify S(i,j) subscript
pairs, which determine the placement of the values in v into
the output. If either i or j is
a vector or matrix, then the other input can be a scalar or can be
a vector or matrix with the same number of elements. In that case, sparse uses i(:) and j(:) as
the subscripts. If i and j have
identical values for several elements in v, then
those elements are added together.

Note

If any value in i or j is
larger than 2^31-1 for 32-bit platforms, or 2^48-1 on
64-bit platforms, then the sparse matrix cannot be constructed.

Data Types: double | logical

v — Valuesscalar | vector | matrix

Values, specified as a scalar, vector, or matrix. If v is
a vector or matrix, then one of the inputs i or j must
also be a vector or matrix with the same number of elements.

Any elements in v that are zero are ignored,
as are the corresponding subscripts in i and j.
However, if you do not specify the dimension sizes of the output, m and n,
then sparse calculates the maxima m =
max(i) and n = max(j) before ignoring
any zero elements in v.

Data Types: double | logicalComplex Number Support: Yes

m,n — Size of each dimension (as separate arguments)integer values

Size of each dimension, specified as separate arguments of integer
values. If you specify m (the row size), you also
must specify n (the column size).

If you do not specify m and n,
then sparse uses the default values m
= max(i) and n = max(j). These maxima
are computed before any zeros in v are removed.

Data Types: double

nz — Storage allocation for nonzero elementspositive integer scalar

Storage allocation for nonzero elements, specified as a positive
integer scalar. nz must be greater than or equal
to max([numel(i), numel(j), numel(v), 1]).

For a sparse matrix, S, the nnz function returns the number of nonzero
elements in the matrix, and the nzmax function
returns the amount of storage allocated for nonzero matrix elements.
If nnz(S) and nzmax(S) return
different results, then more storage might be allocated than is actually
required. For this reason, set nz only in anticipation
of later fill-in.

If you do not specify nz, the sparse function
uses a default value of max([numel(i), numel(j), numel(v),
1]).

accumarray adds elements that have
identical subscripts into the output by default, but can optionally
apply any function to the bins. sparse only adds
elements that have identical subscripts into the output.