MATLAB Answers

How to solve a simple if loop

1 view (last 30 days)
Patty Oikawa
Patty Oikawa on 15 Dec 2012
I have a variable that ranges between 0.01 and 0.62. I want to find all values of that single vector that exceed 0.59 and make them equal 0.59.
I tried running an if statement and it runs with no errors, but the variable does not change (there are still #s in that variable that are greater than 0.6). the code I have now that doesn't work:
if Theta>0.59
then (Theta=0.59);
What am I doing wrong?


Sign in to comment.

Accepted Answer

Matt Fig
Matt Fig on 15 Dec 2012
Edited: Matt Fig on 15 Dec 2012
  1. There is no such thing as an IF loop.
  2. IF statements do not filter elements out of an array.
  3. MATLAB does not use a THEN keyword.
Use this instead:
Theta = min(Theta,.59);
You could also do it this way:
Theta(Theta>.59) = .59;


Walter Roberson
Walter Roberson on 15 Dec 2012
4. Assignment statements cannot be with ().
Matt Fig
Matt Fig on 15 Dec 2012
Good catch, Walter.

Sign in to comment.

More Answers (0)

Sign in to answer this question.