if conditional 2 out of 3

2 visualizaciones (últimos 30 días)
Zeynab Mousavikhamene
Zeynab Mousavikhamene el 11 de Mayo de 2020
Comentada: Walter Roberson el 12 de Mayo de 2020
I have three groups of number which can be 0 or 1. like A, B and C. I want to write a conditional to be true if 2 of three values were 1. How can I do that?
The simplest way would be like:
if (A==1 && B==1) or (A==1 && C==1) or (B==1 && c==1)
end
but I am searching for sth shorter.
Any idea?

Respuesta aceptada

KALYAN ACHARJYA
KALYAN ACHARJYA el 11 de Mayo de 2020
Editada: KALYAN ACHARJYA el 11 de Mayo de 2020
  2 comentarios
Zeynab Mousavikhamene
Zeynab Mousavikhamene el 12 de Mayo de 2020
Thank you. well I am trying to find systematic answer. What would be the conditional design in this cas regardless of valuess A, B and C have.
Walter Roberson
Walter Roberson el 12 de Mayo de 2020
If the values can be something other than 0 and 1, it is not clear how you want to compare them.
(A~=0) + (B~=0) + (C~=0) >= 2 %non-zeros
or
nnz([A,B,C]) >= 2 %non-zeros
or
sum([A,B,C] == SomeValue) >= 2 %match SomeValue specifically
or
nnz([A,B,C] == SomeValue) >= 2 %match SomeValue specifically

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Conditional Mean Models en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by