How to use if statements with arrays?
5 ビュー (過去 30 日間)
古いコメントを表示
I am trying to use an if statement with an array,
x = 1:1:4
if x > 2
y = x + 1
else
y = x - 1
end
I want the resulting array for y to be [ 0, 1, 4, 5], how would i execute specific functions for only certain numbers in an array? i want y = x + 1 to only affect the numbers in the x array that are greater then 2 and y = x - 1 to only affect the numbers in the x array that are less then 2.
0 件のコメント
採用された回答
Brian B
2013 年 2 月 12 日
y = (x>2).*(x+1) + (x<=2).*(x-1)
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Matrices and Arrays についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!