nnz
Number of non-zero elements.
Syntax
Section titled “Syntax”n = nnz(X)Description
Section titled “Description”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.
Examples
Section titled “Examples”nnz(speye(100)) % 100nnz([1 0 2 0 3 0]) % 3nnz(zeros(5)) % 0