Skip to content

spalloc

Allocate space for an m × n sparse matrix.

S = spalloc(m, n, nzmax)

Returns an empty m × n sparse matrix with storage pre-allocated for nzmax non-zero entries. Doesn’t insert any values — a placeholder for a sparse matrix you’ll fill incrementally without triggering reallocations on each assignment.

S = spalloc(1000, 1000, 50); % allocate for up to 50 nonzeros
for i = 1:50
S(i, i) = i;
end
  • sparse — Create a sparse matrix.
  • nnz — Number of non-zero elements.
  • nzmax — Storage allocated for non-zero elements.