nnz

Purpose

Number of nonzero entries.

Synopsis

nz = nnz(X)

Description

nz = nnz(X) is the number of nonzeros in matrix X.

The density of a sparse matrix, which is printed by the whos command, is nnz(X)/prod(size(X)).

Examples

The matrix

 w = sparse(wilkinson(21));
is a tridiagonal matrix with 20 nonzeros on each of three diagonals, so nnz(w) = 60.

See Also

find, issparse, nonzeros, nzmax, size, whos

(c) Copyright 1994 by The MathWorks, Inc.