필터 지우기
필터 지우기

How do count the number under multiple conditions?

조회 수: 1 (최근 30일)
Wan-Yi Chiu
Wan-Yi Chiu 2021년 9월 10일
편집: Image Analyst 2021년 9월 10일
I have four numbers (A, B, C, D) that may be different. I would like to count the number such as A=B=C=D, A~=B~=C~=D, or A=B, C=D, but A~=C, etc. My codes are as follows, but the answers are wrong. Please check why?
Thank you very much.
A=3; B=3; C=3; D=3; %% Case-1 The answer should be Q1=1 and Q2=...Q16=0
Q1=sum(A==B==C==D)
Q2=sum(A~=B~=C~=D)
Q3=sum(A==B & C==D & A~=C)
Q4=sum(A==C & B==D & A~=B)
Q5=sum(A==D & B==C & A~=B)
Q6=sum(A==B==C & C~=D)
Q7=sum(A==B==D & C~=D)
Q8=sum(A==C==D & A~=B)
Q9=sum(B==C==D & A~=D)
Q10=sum(A==B==D & A~=C)
Q11=sum(A==B & C~=D & A~=C)
Q12=sum(A==C & B~=D & A~=B)
Q13=sum(A==D & B~=C & A~=B)
Q14=sum(B==C & A~=D & B~=A)
Q15=sum(B==D & A~=C & B~=A)
Q16=sum(C==D & A~=B & C~=A)
A=3; B=3; C=3; D=5; %% Case-2 The answer should be Q6=1 and others=0

채택된 답변

Image Analyst
Image Analyst 2021년 9월 10일
편집: Image Analyst 2021년 9월 10일
Use && instead of &.
And you can only compare pairs, so you can't do
Q1=sum(A==B==C==D)
but you can do
Q1=sum(A==B && A==C && A==D && B==C && B==D && C==D)
Of course it's best if you just use histogram() and look at the counts in each bin.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

태그

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by