How to get If Then statements to work on a vector

Hi. (Relatively) new to Matlab.
I try this code
x=[4 5 6]
if x>5
y=5
else
y=x
end
expecting y to return y=4 5 5
but instead it returns y= 4 5 6
What am I doing wrong with this IF statement? How can I do elementwise logical statements? I have searched for this but have found nothing helpful.
Thanks

 採用された回答

David Young
David Young 2014 年 9 月 30 日

0 投票

y = x;
y(x > 5) = 5;

1 件のコメント

Thomas
Thomas 2014 年 9 月 30 日
That works. Cool, thanks.

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

その他の回答 (0 件)

カテゴリ

質問済み:

2014 年 9 月 30 日

コメント済み:

2014 年 9 月 30 日

Community Treasure Hunt

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

Start Hunting!

Translated by