MATLAB: Violation of logical indexing criteria problem

absolute valueindex

Hi everyone. I am collecting grid points (alpha) from a larger grid (x) by the following commands:
I = abs(x)<0.5;
alpha = x(I);
My problem is that this criteria is not being able to filter x = -0.5. My question is, does Matlab think abs(-0.5)>0.5?
What am i doing wrong here? Please help.

Best Answer

Your value is probably not being calculated as exactly -0.5 . -0.5 is exactly representable in binary floating point, but different ways of calculating what "should" be -0.5 do not always give that exact value. If you add 0.5 to the value you think should be -0.5 you will probably find that the result is not exactly 0.
Related Question