How to get If Then statements to work on a vector

1 vue (au cours des 30 derniers jours)
Thomas
Thomas le 30 Sep 2014
Commenté : Thomas le 30 Sep 2014
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

Réponse acceptée

David Young
David Young le 30 Sep 2014
y = x;
y(x > 5) = 5;

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by