OR, Matrices and equality
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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
2018년 2월 2일
편집: James Tursa
2018년 2월 2일
1 개 추천
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 ||?
James Tursa
2018년 2월 2일
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.
Bill Bulgari
2018년 2월 2일
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.
James Tursa
2018년 2월 2일
편집: James Tursa
2018년 2월 2일
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.
Bill Bulgari
2018년 2월 2일
편집: James Tursa
2018년 2월 2일
I am really sorry but I don't understand how we get [1 1 1] in the last step.
James Tursa
2018년 2월 2일
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.
Bill Bulgari
2018년 2월 2일
편집: James Tursa
2018년 2월 2일
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.
Bill Bulgari
2018년 2월 2일
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!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
