Hi,
I am seeing a strange result in matlab using the OR operator.
This is what I am seeing:
K>> 5==1|2
ans =
1
I must be doing something wrong, but can't figure out what. Any ideas?

1 件のコメント

José-Luis
José-Luis 2014 年 7 月 8 日
What did you expect to get?

サインインしてコメントする。

 採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 7 月 8 日

2 投票

This is correct
5==1
The result is 0
0|2
the result is 1

4 件のコメント

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 7 月 8 日
Maybe you want
(5==1)|(5==2)
which gives 0
Adam
Adam 2014 年 7 月 9 日
You are correct, I needed the 5==1|5==2 format. Thank you for your response.
Just out of curiosity, what is the other format doing, i.e. why does 5==1|2 result in true?
Azzi Abdelmalek
Azzi Abdelmalek 2014 年 7 月 9 日
5==1|2
is the same then
(5==1)|2
Matlab consider each number different from zero as true
5==1 % is false
2 % is true
false | true %will give true
James Tursa
James Tursa 2014 年 7 月 9 日
編集済み: James Tursa 2014 年 7 月 9 日
5==1 is done first and results in a logical value 0 (i.e., false)
Then 0|2 is done next. It is a logical OR and also returns a logical result. If either of the inputs is non-zero, then the result will be 1 (i.e., true), so that is what you get for your example.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeEntering Commands についてさらに検索

製品

タグ

質問済み:

2014 年 7 月 8 日

編集済み:

2014 年 7 月 9 日

Community Treasure Hunt

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

Start Hunting!

Translated by