This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this 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.

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

See Also

| | | |

Introduced before R2006a

Was this topic helpful?