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

21 次查看(过去 30 天)
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 个评论
Stephen23
Stephen23 2016-2-26
编辑:Stephen23 2016-2-26
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 2016-2-26
编辑:Adam 2016-2-26
Judging by your use of | rather than | | I suspect you may also want & rather than &&

请先登录,再进行评论。

采纳的回答

Kevin Claytor
Kevin Claytor 2016-2-26
编辑:Kevin Claytor 2016-2-26
| 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 个评论
Stephen23
Stephen23 2016-2-26
编辑:Stephen23 2016-2-26
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 2016-2-26
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.

请先登录,再进行评论。

更多回答(1 个)

Stephen23
Stephen23 2016-2-26
编辑:Stephen23 2016-2-26
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.

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by