OR, Matrices and equality

M=[0 1 0];
w=M==[1 1 0] | Μ==[1 1 1]
w =
1 1 1
I wanted to get a resuilt that show this statemant is wrong. Can somebody explane why that didn't happen?

 采纳的回答

James Tursa
James Tursa 2018-2-2
编辑:James Tursa 2018-2-2
The == and | operations you are doing are element-wise operations, so you get an element-wise result according to the precedence of the operators. To do what you appear to want to do, compare as entire rows, you could do this:
w = ismember(M,[1 1 0;1 1 1],'rows');
Or in a more verbose fashion,
w = isequal(M,[1 1 0]) || isequal(M,[1 1 1]);

9 个评论

So what I did is: M(1,1)==1 or 1, M(1,2)==1 or 1 and M(1,3)==0 or 1 and for eah statement I assign the appropriate value to the corresponding element of the 3x3 matrix w? Is there a reason | has turned into ||?
Please copy & paste the exact code you are using, not a description of your code. I could comment on your description, but it wouldn't really mean much without seeing the code itself.
I have included the code in the question. I just asked if what you meant in your answer that I did is what I discribed in the commend.
Ah, I misunderstood. Yes, the M==[1 1 0] statement does the equivalent of
[M(1,1)==1,M(1,2)==1,M(1,3)==0]
Then that 3x1 result is element-wise OR'ed with the [1,1,1] vector. So as a result you get:
[ (M(1,1)==1) | 1 , (M(1,2)==1) | 1 , (M(1,3)==0) | 1 ]
Which of course will always give the result [1,1,1]
The reason I switched from the element-wise "|" to the logical "||" operator is because you are doing a logical test of equality, and the "||" makes more sense in this context. If the operands are scalars then you get the same result, but it is probably better programming practice to use "&&" and "||" in your if tests because that is almost always what you mean when coding them. By using "&&" and "||" you will get an error if you inadvertently use non-scalars for operands ... and this is what you want to have happen. You want the code to error and tell you that you did something wrong and point you to the exact line where you had incorrect code, rather than the code continuing and simply producing a wrong answer and then leaving you to debug to find out what and where things went wrong.
I am really sorry but I don't understand how we get [1 1 1] in the last step.
Because (anything) | 1 always results in 1. It doesn't matter what calculations you did with M to get the (anything) part, since you OR each element with 1 you will get a 1 in each element for the result.
And what's the difference with "||"?
In my code, nothing, because the operands are scalars (the result of the isequal( ) function is a scalar). But I always use "||" in these cases per the reasons I cite above.
For your case, consider what would have happened if you had used the logical "||" operator instead of the element-wise "|" operator:
>> M=[0 1 0]
M =
0 1 0
>> w=M==[1 1 0] || M==[1 1 1]
??? Operands to the || and && operators must be convertible to logical scalar values.
You get an error. That is a very good thing! MATLAB told you that you did something wrong, and pointed out the line where you did it. This is much easier to debug than what you did above where you simply got a wrong answer. What if that erroneous line had been buried inside thousands of lines of code? It would be much harder to track down the problem.
Bottom line: My advice to you is to always use the logical "&&" and "||" operators with if tests because that is almost always what you mean when you write the code.
Ok. Thank you very much for your answers. You were really helpful.

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Logical 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by