|MATLAB Function Reference|
Create sparse matrix
sparse function generates matrices in the MATLAB sparse storage organization.
S = sparse(A)
converts a full matrix to sparse form by squeezing out any zero elements. If
S is already sparse,
S = sparse(i,j,s,m,n,nzmax)
s to generate an
n sparse matrix such that
S(i(k),j(k)) = s(k), with space allocated for
nzmax nonzeros. Vectors
s are all the same length. Any elements of
s that are zero are ignored, along with the corresponding values of
j. Any elements of
s that have duplicate values of
j are added together.
If any value in |
To simplify this six-argument call, you can pass scalars for the argument
s and one of the arguments
j--in which case they are expanded so that
s all have the same length.
S = sparse(i,j,s,m,n)
S = sparse(i,j,s)
max(j). The maxima are computed before any zeros in
s are removed, so one of the rows of
s] might be
S = sparse(m,n)
sparse(,,,m,n,0). This generates the ultimate sparse matrix, an
n all zero matrix.
All of the MATLAB built-in arithmetic, logical, and indexing operations can be applied to sparse matrices, or to mixtures of sparse and full matrices. Operations on sparse matrices return sparse matrices and operations on full matrices return full matrices.
In most cases, operations on mixtures of sparse and full matrices return full matrices. The exceptions include situations where the result of a mixed operation is structurally sparse, for example,
A.*S is at least as sparse as
sparse(1:n,1:n,1) generates a sparse representation of the
n identity matrix. The same
S results from
sparse(eye(n,n)), but this would also temporarily generate a full
n matrix with most of its elements equal to zero.
sparse(10000,10000,pi) is probably not very useful, but is legal and works; it sets up a
10000 matrix with only one nonzero element. Don't try
full(B); it requires 800 megabytes of storage.
This dissects and then reassembles a sparse matrix:
So does this, if the last row and column have nonzero entries: