How can I say if (A & B) or C in Matlab?
Mostrar comentarios más antiguos
Hi! I have three conditions A, B and C. I would like to set: if both A and B or C in Matlab. I thought to:
if (A && B) | C
but It doesn't work. How can i do it? Thanks!
2 comentarios
It works for me:
>> if false | (true & true), disp('ok'), end
ok
>> if false | (true & false), disp('ok'), end
>>
What code did you try ? What size are A, B, and C ?
Respuesta aceptada
Más respuestas (1)
You need to consider the size of your operands. You have not told us their sizes, but this is very important information for us to know what you should do.
This is explained quite clearly in the if documentation: "An expression is true when its result is nonempty and contains only nonzero elements (logical or real numeric). Otherwise, the expression is false."
Which means:
- if [0,0,0,0] is false: all values are zero
- if [0,1,0,1] is false: some values are zero
- if ~[0,0,0,0]=[1,1,1,1] is true: contains only nonzero elements
- if ~[0,1,0,1]=[1,0,1,0] is false: some values are zero
Note that the definition defines a true expression as "contains only nonzero elements" but does not give an upper-limit to the number of nonzero elements. It is likely that you have multiple elements, some of which are false. then your if will not run.
Categorías
Más información sobre Functions 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!