Is there a function that, once introduced any matrix, counts how many 0's there are, how many elements between 0 and 1, and how many are bigger than 1 in said matrix? Thanks!

 Akzeptierte Antwort

Kelly Kearney
Kelly Kearney am 9 Jan. 2014

1 Stimme

x = rand(100);
n0 = sum(x(:) == 0);
n0to1 = sum(x(:) > 0 & x(:) <= 1);
ngt1 = sum(x(:) > 1);

Weitere Antworten (1)

Matt J
Matt J am 9 Jan. 2014
Bearbeitet: Matt J am 9 Jan. 2014

1 Stimme

You can do
nnz(A>1)
or
sum(A(:)>1)
and similarly for other logical conditions. In some cases, nnz will be the fastest option for sparse matrices. Otherwise, sum() is usually the fastest.

Kategorien

Mehr zu Sparse Matrices finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by