How do I make an if, elseif, else statement?
562 ビュー (過去 30 日間)
古いコメントを表示
if 0<x<10
y=4*x
elseif 10<x<40
y = 10*x
else
y = 500
end
I would expect test cases of
x= -1
x= 5
x= 30
x=100
2 件のコメント
Walter Roberson
2011 年 6 月 14 日
Good point, Jan.
zizo, Jan's point here is one that I pointed out to you before, when you asked a question a month ago, http://www.mathworks.com/matlabcentral/answers/7479-just-q
採用された回答
Paulo Silva
2011 年 6 月 14 日
The correct way to do the conditions is this:
if 0<x & x<10
y=4*x
elseif 10<x & x<40
y=10*x
else
y=500
end
0 件のコメント
その他の回答 (1 件)
Sean de Wolski
2011 年 6 月 14 日
Or the vectorized solution:
y = repmat(500,size(x));
idx = 0<x&x<10;
y(idx) = 4*x(idx);
idx = 10<x&x<40;
y(idx) = 10*x(idx);
3 件のコメント
Sean de Wolski
2011 年 6 月 14 日
Is it just the order of operations that worries you?
BTW. I'm not even sure my approach would be faster than with the accelerated for-loops.
参考
カテゴリ
Help Center および File Exchange で Introduction to Installation and Licensing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!