Navigation

Operators and Keywords

Function List:

C++ API

: s = spalloc (m, n, nz)

Create an m-by-n sparse matrix with pre-allocated space for at most nz nonzero elements.

This is useful for building a matrix incrementally by a sequence of indexed assignments. Subsequent indexed assignments after spalloc will reuse the pre-allocated memory, provided they are of one of the simple forms

  • s(I:J) = x
  • s(:,I:J) = x
  • s(K:L,I:J) = x

and that the following conditions are met:

  • the assignment does not decrease nnz (S).
  • after the assignment, nnz (S) does not exceed nz.
  • no index is out of bounds.

Partial movement of data may still occur, but in general the assignment will be more memory and time efficient under these circumstances. In particular, it is possible to efficiently build a pre-allocated sparse matrix from a contiguous block of columns.

The amount of pre-allocated memory for a given matrix may be queried using the function nzmax.

See also: nzmax, sparse.

Package: octave