Suppose we have an array r=(2,2,2) filled with zeros and ones. How to count up how many zeros and how many ones do we have?

 채택된 답변

Image Analyst
Image Analyst 2016년 9월 22일

1 개 추천

Try this:
numOnes = sum(r(:))
numZeros = numel(r) - numOnes

댓글 수: 4

Vadim Tambovtsev
Vadim Tambovtsev 2016년 9월 22일
No, my array in more complicated. Array (m,n,k), contans "a" and "b" values. How many "a" and "b" are there?
Image Analyst
Image Analyst 2016년 9월 22일
편집: Image Analyst 2016년 9월 22일
You could have sped this along by two hours by saying that first instead of saying the array is "filled with zeros and ones". So to count the number of a's and b's:
numA = length(find(r==a));
numB = length(find(r==b));
There are other ways, but this is fairly simple and compact.
Vadim Tambovtsev
Vadim Tambovtsev 2016년 9월 22일
편집: Image Analyst 2016년 9월 22일
quality control:
dd=[ 3 7; 4 5]
num7=sum(find(dd==7))
output:
num7= 3
Why is it 3? isn't it supposed to be 1, because there is 1 "7" in the array?
Image Analyst
Image Analyst 2016년 9월 22일
Use length() instead of sum().

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Numerical Integration and Differential Equations에 대해 자세히 알아보기

태그

질문:

2016년 9월 22일

편집:

2016년 9월 22일

Community Treasure Hunt

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

Start Hunting!

Translated by