MATLAB: What is the reason for this error and how can i solve it

logical indexing

I am trying to add a value to a number if the number in the row > 0.5. Why is it that the following syntax with x = [0.1 0.2 0.3 0.7 0.8 0.9 0.1 0.2 0.3]; works with: x(x~=0)=x+1 (giving 1.1000 1.2000 1.3000 1.7000 1.8000 1.9000 1.1000 1.2000 1.3000) but not with: x(x>0.5)=x+1.
My goal is to add 1 to every value exceeding 0.5 Thank you

Best Answer

Your x(x~=0)=x+1 only works because there are no 0 value in your x. It will fail with x = [0 0.1].
The correct syntax is:
x(x~=0) = x(x~=0)+1
x(x>0.5) = x(x>0.5)+1
That is, you need to do the filtering on both sides of the equal so that you have the same number of elements each side.