Using logical operator in an if condition
Mostrar comentarios más antiguos
Hi, I want my 'if' condition as follows:
if true
if (x==1) && (A(2,1)==1 || A(2,2)==1 || A(2,3)==1)
...
end
(Please disregard the first line with 'if true' )
I mean, I want my condition to be x equals 1 and one of A(2,1) A(2,2) or A(2,3) to be equal 1 as well. I think, the way that the expression above is written, is incorrect because even the condition isn't satisfied, program continues to execute the if statement.
How should I write the above condition?
Thank you Kaan Yilmaz
Respuestas (2)
Roger Stafford
el 5 de Jun. de 2016
Editada: Roger Stafford
el 5 de Jun. de 2016
0 votos
You are using the "short circuit" logical operators. They work for scalars but not for vectors with more than one element. In the latter case change to '&' and '|'.
Another possibility for your difficult is that you are requiring exact equality which may not be satisfied if x, y, z, or t differ from 1 in their least binary digits from rounding errors.
2 comentarios
Kaan Yilmaz
el 5 de Jun. de 2016
Editada: Kaan Yilmaz
el 5 de Jun. de 2016
Roger Stafford
el 5 de Jun. de 2016
Editada: Roger Stafford
el 5 de Jun. de 2016
I would like to see your actual code that you are using to produce the wrong result, including how A is generated.
aakash jain
el 4 de Mzo. de 2021
0 votos
Error in untitled1233 (line 4)
if Z == 0 || X==P
ERROR
Categorías
Más información sobre Shifting and Sorting Matrices en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!