How do count the number under multiple conditions?
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Wan-Yi Chiu
am 10 Sep. 2021
Bearbeitet: Image Analyst
am 10 Sep. 2021
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
0 Kommentare
Akzeptierte Antwort
Image Analyst
am 10 Sep. 2021
Bearbeitet: Image Analyst
am 10 Sep. 2021
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 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!