How to get If Then statements to work on a vector

조회 수: 1 (최근 30일)
Thomas
Thomas 2014년 9월 30일
댓글: Thomas 2014년 9월 30일
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일
y = x;
y(x > 5) = 5;

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Entering Commands에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by