if-else statement with several "or" conditions/ comparisons

2 ビュー (過去 30 日間)
rbme17
rbme17 2017 年 7 月 10 日
コメント済み: rbme17 2017 年 7 月 11 日
Hi,
So I'm writing an if-else statement, which includes an "or" condition. I wanted to write a statement that gives me the option of checking not only two variables with "or", but a whole array of variables/numbers [a, b, c, d, e...]
if x == a | b --> multiple variables without a ton of code
Sample of my code:
for x = 1:50
Cf(x).yB1 = rand();
if x == a| x == b
Cof(x).fB20 = 0;
Cof(x).fB10 = -2*pi;
else
Cf(x).B10 = -(2*pi/5) * (rand());
Cf(x).B20 = (2*pi/20) * (rand());
Cf(x).B0 = (rand()) - 0.0003 * x^2;
end
end
I'm not entirely sure if what I'm asking is feasible, and if not, does anyone have any other ideas on how I can accomplish this?
Thanks!

採用された回答

Walter Roberson
Walter Roberson 2017 年 7 月 10 日
if ismember(x, [a, b, c, ...])
  2 件のコメント
dpb
dpb 2017 年 7 月 10 日
And if will put the various variables into an array that'll reduce code even more and probably help elsewhere besides just here.
rbme17
rbme17 2017 年 7 月 11 日
Okay, thanks so much for the help!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by