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 Center および File ExchangeLogical についてさらに検索

タグ

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by