0

Can someone explain me why these code lines

    x = 0:0.001:1;
    find(x==0.9)

return 901, while these code lines

   x = 0:0.001:1.4;
   find(x==0.9)

return en empty matrix?

I know Matlab can involve in some approximation errors, but in this case it doesn't seem to make sense. The surprising thing is that if x goes from 0 to an integer number then Matlab represents each element of the vector without approximation errors. While if x goes from 0 to a decimal number, it doesn't work. Is there a reason why it happens?

Alessandro
  • 764
  • 3
  • 8
  • 22
  • This is happening due to floating point approximations. You can read more about it [here](https://www.mathworks.com/help/matlab/matlab_prog/floating-point-numbers.html) – Some Guy Nov 22 '17 at 14:33
  • The surprising thing is that if x goes from 0 to an integer number then Matlab represents each element of the vector without approximation errors. While if x goes from 0 to a decimal number, it doesn't work. Is there a reason why it happens? – Alessandro Nov 22 '17 at 14:38
  • @Alessandro Looks can be deceiving. Try changing the data representation in MATLAB. Do `format long g;` then display your data again. Simply put, it happens due to the representation of the data for display in MATLAB. – rayryeng Nov 22 '17 at 15:36

0 Answers0