WEIRD PROBLEM: cannot find 28.88 in an array

1 view (last 30 days)
E = 14:0.01:141;
find(E==28.88);
Please run the code above. The result is 'empty'? It's odd. Is this a bug in Matlab or my problem?

Accepted Answer

Star Strider
Star Strider on 2 Sep 2015
It’s neither a bug nor your problem. It’s a combination of the inexact representation of decimal numbers as binary floating-point numbers, and the way MATLAB calculates colon-operator-generated vectors. So you have to allow for tolerances.
This works:
E = 14:0.01:141;
idx1 = find(E<=28.88, 1, 'last');
idx2 = find(E>=28.88, 1, 'first');
Result = E([idx1 idx2])
Result =
28.8700 28.8800

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!