MATLAB: Issue with find() function

find

If I create an array x = 0.1:0.05:0.6; and then ask find(x==0.15), I get an empty matrix. It also fails for x==0.40? I don't understand this at all.

Best Answer

It’s called ‘floating point approximation error’. The concept is similar to the decimal approximation of 1/3 = 0.33333.... To get around it, you need to state the conditions in your find calls to allow for some imprecision.
Run these to get an idea of how the concept applies to your problem:
x = 0.1:0.05:0.6;
ix11 = find(x <= 0.15, 1, 'last')
ix12 = find(x >= 0.15, 1, 'first')
x1 = x([ix11 ix12])
ix21 = find(x <= 0.40, 1, 'last')
ix22 = find(x >= 0.40, 1, 'first')
x2 = x([ix21 ix22])