Counting certain values from an array

조회 수: 1 (최근 30일)
Vadim Tambovtsev
Vadim Tambovtsev 2016년 9월 22일
편집: Image Analyst 2016년 9월 22일
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일
Try this:
numOnes = sum(r(:))
numZeros = numel(r) - numOnes
  댓글 수: 4
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개)

카테고리

Help CenterFile Exchange에서 Numerical Integration and Differential Equations에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by