How do count the number under multiple conditions?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Wan-Yi Chiu
el 10 de Sept. de 2021
Editada: Image Analyst
el 10 de Sept. de 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 comentarios
Respuesta aceptada
Image Analyst
el 10 de Sept. de 2021
Editada: Image Analyst
el 10 de Sept. de 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 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!