Skip to content

nnz

Number of non-zero elements.

n = nnz(X)

Returns the count of structurally non-zero elements in X. For sparse matrices, this is the count of stored entries (which may include explicit zeros if any were inserted). For full matrices, it counts elements that are not exactly zero.

nnz(speye(100)) % 100
nnz([1 0 2 0 3 0]) % 3
nnz(zeros(5)) % 0
  • nzmax — Storage allocated for non-zero elements.
  • nonzeros — Column vector of non-zero elements.