MATLAB: How am I misunderstanding this if statement

if statement

The code:
x = [1 5 4 2];
if x > 3
m = x+1;
else
m = x - 1;
end
disp(m)
I expect that the resulting vector would be m = [0 6 5 1] since x(2) and x(3) > 3, however I'm getting m = [0 4 3 1]. Can anyone explain why this is?

Best Answer

You need to use logical indexing. MATLAB does not understand what you meant properly. Try this:
x=[1 5 4 2];
x(x>3)=x(x>3)+1;
x(x<=3)=x(x<=3)-1