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
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 件のコメント
Bill Bulgari
2018 年 2 月 2 日
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 "||"?
James Tursa
2018 年 2 月 2 日
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!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)
