How can I say if (A & B) or C in Matlab?

23 views (last 30 days)
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 Comments
Stephen23
Stephen23 on 26 Feb 2016
Edited: Stephen23 on 26 Feb 2016
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 ?
Adam
Adam on 26 Feb 2016
Edited: Adam on 26 Feb 2016
Judging by your use of | rather than | | I suspect you may also want & rather than &&

Sign in to comment.

Accepted Answer

Kevin Claytor
Kevin Claytor on 26 Feb 2016
Edited: Kevin Claytor on 26 Feb 2016
| operates elementwise on arrays. You might want to check the size of A, B, and C. If then operates on the array (See @Stephen's answer).
>> help |
| Logical or.
A | B performs a logical or of arrays A and B and returns an array
containing elements set to either logical 1 (TRUE) or logical 0
(FALSE)....
Note that there are two logical or operators in MATLAB. The |
operator performs an element-by-element or between matrices, while
the || operator performs a short-circuit or between scalar values.
I prefer using && and | | for a range of personal reasons:
  • Certainly when I was starting out, I didn't consider the possibility of the behavior of if operating on a vector. Keeping statements to the scalar operators adds implicit error-checking when you hand your code to someone else - they'll now get an error (&& doesn't operate on vectors) instead of a possibly ill-defined result. Of course, if you intend to operate on vectors, you can ignore this comment.
  • As @Stephen mentioned below, it doesn't have to evaluate the rest of the expression once the condition is met. Just make sure all of your () are in the right places.
  • It forces you to think a bit more about about the intent of the condition, for example do you want: all(A) to be true, or would you be happy if any(A) are true? Even though the default for "if all(A)" is equivalent to "if A", you've just made your intent explicitly clear.
  • People coming from other languages may interpret the intent differently. For example in python;
>> if [0, 0, 0]:
print("yes")
>> yes
(because [0,0,0] is not None.) whereas;
>> if all([0, 0, 0]):
print("yes")
>>
  2 Comments
Stephen23
Stephen23 on 26 Feb 2016
Edited: Stephen23 on 26 Feb 2016
This answer does not make much sense. The main feature of the short-circuiting operators is that they only evaluate as many operands as are required to determine the output value. Here is an example where z does not exist in the workspace:
>> 1==exist('z','var') && z>3 % only first term is evaluated: z does not exist!
ans = 0
now define z and run it again:
>> z = 4;
>> 1==exist('z','var') && z>3 % both terms are evaluated
ans = 1
however the logic is exactly the same as using the normal or operator: there is no difference in the logical operation performed, they are both or's. Of course the normal logical operators can also be used with scalars. Why shouldn't they? As long as the operands are all defined then for scalar values these are logically equivalent:
>> false || (true && true) % short circuit
ans = 1
>> false | (true & true) % normal
ans = 1
You have not explained why | needs to be replaced with ||.
Kevin Claytor
Kevin Claytor on 26 Feb 2016
You're right. I shot that off without thinking too much about it. Upon reflection, I think I was in two places at once; matrix size and personal preference. I've updated my answer to hopefully make the distinction more clear.

Sign in to comment.

More Answers (1)

Stephen23
Stephen23 on 26 Feb 2016
Edited: Stephen23 on 26 Feb 2016
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.

Community Treasure Hunt

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

Start Hunting!