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!

 채택된 답변

Kelly Kearney
Kelly Kearney 2014년 1월 9일

1 개 추천

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

추가 답변 (1개)

Matt J
Matt J 2014년 1월 9일
편집: Matt J 2014년 1월 9일

1 개 추천

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.

카테고리

도움말 센터File Exchange에서 Sparse Matrices에 대해 자세히 알아보기

질문:

2014년 1월 9일

편집:

2014년 1월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by