Skip to Main Content Skip to Search
Product Documentation

nnz - Number of nonzero matrix elements

Syntax

n = nnz(X)

Description

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

The density of a sparse matrix 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 | isa | nonzeros | nzmax | size | whos

  


» Learn more
» Download free kit
» Get trial software

 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS