How do I use a single or double pipe (vertical line) in an "or" statement?

73 ビュー (過去 30 日間)
Sabbas
Sabbas 2012 年 7 月 2 日
I have the following code
if k==39 or K==90
do that
else
do that
end
My question is how to replace the or statement
With | or with the double vertical line

採用された回答

Luffy
Luffy 2012 年 7 月 2 日
if k==39 || K==90
do that
else
do that
end

その他の回答 (1 件)

Daniel Shub
Daniel Shub 2012 年 7 月 2 日
There are two main differences between | and . The first difference is that "short circuits", while | does not. This means that you can save yourself 1 comparison every time the if statement is evaluated. Assuming equal over head for | and , this means you can save some computation time. How much time you save depends on what proportion of the time your code spends checking for k equal to 39 takes. The second difference is that is a relatively new addition to MATLAB. I cannot easily find when they were introduced ( Loren's blog uses them in 2006). If you want your code to work with older versions of MATLAB, you cannot use .
Since I doubt that k==90 will be the bottle neck in your code, I would go with | for backwards compatibility.
  2 件のコメント
Jan
Jan 2012 年 7 月 2 日
編集済み: Jan 2012 年 7 月 2 日
|| works at least since Matlab 6.5.
Sabbas
Sabbas 2012 年 7 月 2 日
編集済み: Sabbas 2012 年 7 月 2 日
So the bottom line is that i can still use the double vertical line ?

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

カテゴリ

Help Center および File ExchangeTables についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by