MATLAB: Find function with floating points number

findfunction

distances=[100.2545 120.6632;200.8588 220.4465]
points_id=[{'p1'} {'p2'};{'p3'} {'p4'}]
pointfind = @(d) points_id(find(distances == d));
p = pointfind(120.6632)
%it gives
p1 =
Empty cell array: 0-by-1
%this function only works with integers but I need to work with floating points number.

Best Answer

This revision of pointfind should work:
pointfind = @(d) points_id(find(fix(distances*10^(length(num2str(rem(d,1)))-2))/(10^(length(num2str(rem(d,1)))-2)) == d));
(It obviously all goes on one line, although it wraps here.)
You are dealing with differences in precision between the values stored in distances and the precision in the argument ā€˜dā€™ you present to pointfind. The revised version of pointfind truncates the elements of distances to the precision of ā€˜dā€™ before it does the comparison. It works with decimal fractions now.