This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.


Number of nonzero matrix elements


n = nnz(X)


n = nnz(X) returns the number of nonzero elements in matrix X.

The density of a sparse matrix is nnz(X)/prod(size(X)).


The matrix

 w = sparse(wilkinson(21));

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

More About

collapse all

Tall Array Support

This function fully supports tall arrays. For more information, see Tall Arrays.

See Also

| | | | |

Introduced before R2006a

Was this topic helpful?