sparse
Create a sparse matrix.
Syntax
Section titled “Syntax”S = sparse(A)S = sparse(m, n)S = sparse(i, j, s)S = sparse(i, j, s, m, n)S = sparse(i, j, s, m, n, nzmax)Description
Section titled “Description”Builds a sparse matrix. Three main call forms:
sparse(A)— convert a full matrixAto sparse representation. Already-sparse inputs are returned unchanged.sparse(m, n)— build an emptym × nsparse matrix (all zeros) with default precision.sparse(i, j, s, [m, n, [nzmax]])— build from triplets: vectorsi(row indices),j(column indices),s(values). Optionalm,nset explicit dimensions; optionalnzmaxreserves storage for that many non-zeros.
Sparse matrices store only non-zero entries; arithmetic and indexing operations preserve sparsity where possible.
Examples
Section titled “Examples”S = sparse(eye(1000)); % 1000×1000 sparse identityS = sparse([1 2 3], [1 2 3], [10 20 30]); % 3×3 diagonalS = sparse(5, 5); % 5×5 empty sparseSee also
Section titled “See also”full— Convert a sparse matrix to full storage.spalloc— Allocate space for anm × nsparse matrix.speye— Sparse identity matrix.sprand— Sparse uniform random matrix.issparse— Test whether the input is a sparse matrix.nnz— Number of non-zero elements.nzmax— Storage allocated for non-zero elements.