Skip to content

sparse

Create a sparse matrix.

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)

Builds a sparse matrix. Three main call forms:

  • sparse(A) — convert a full matrix A to sparse representation. Already-sparse inputs are returned unchanged.
  • sparse(m, n) — build an empty m × n sparse matrix (all zeros) with default precision.
  • sparse(i, j, s, [m, n, [nzmax]]) — build from triplets: vectors i (row indices), j (column indices), s (values). Optional m, n set explicit dimensions; optional nzmax reserves storage for that many non-zeros.

Sparse matrices store only non-zero entries; arithmetic and indexing operations preserve sparsity where possible.

S = sparse(eye(1000)); % 1000×1000 sparse identity
S = sparse([1 2 3], [1 2 3], [10 20 30]); % 3×3 diagonal
S = sparse(5, 5); % 5×5 empty sparse
  • full — Convert a sparse matrix to full storage.
  • spalloc — Allocate space for an m × n sparse 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.