Find the index of a row within a larger array while allowing for a tolerance. Other built-in functions do not allow you to specify a tolerance. Determines distance based on a Euclidean distance
Justin Winokur (2020). find_closest_row (https://www.mathworks.com/matlabcentral/fileexchange/40473-find_closest_row), MATLAB Central File Exchange. Retrieved .
Determines distance based on a Euclidean distance！
A relative tolerance can be useful, but as usual for small values (e.g. 1e-20) further problems will occur.
I think it is a more important problem, that your function does not perform what its name advertises: Not the *closest* row is replied, but the first one inside the tolerance. Although a message is displayed, I'd prefer to use another name or algorithm, e.g.:
[d, ind] = min(sum(bsxfun(@minus, full_array, row) .^ 2, 2));
if d > tol, ind = -1; end
Replying the empty matrix could be more intuitive than -1, when no matching row was found.
Those are interesting points that I hadn't considered, though you would probably want to be more careful with how you specify tolerances anyway when you work with such large numbers.
Thanks for the feedback
"(min(distance)-tol)<=eps" is a bad comparison: Imagine, that the distances and tolerance has a magnitude of 1e80. Then comparing with EPS is not sufficient - eps(tol) would be smarter.
Instead of calculating the square root of the distances, it would be much more efficient to compare the squared distances with the squared tolerance.
This would reply the row with the smallest distance, not the first one below the tolerance:
[value, found_index] = min(distance - tol)