How do count the number under multiple conditions?

1 次查看(过去 30 天)
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