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

49 views (last 30 days)
Adriano on 26 Feb 2016
Edited: Kevin Claytor on 26 Feb 2016
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!
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
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]:
>> yes
(because [0,0,0] is not None.) whereas;
>> if all([0, 0, 0]):
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 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!