File Exchange

## find_closest_row

version 1.0.0.0 (1.59 KB) by Justin Winokur

### Justin Winokur (view profile)

Find the index of a row within a larger array while allowing for a tolerance

Updated 22 Feb 2013

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

### Cite As

Justin Winokur (2020). find_closest_row (https://www.mathworks.com/matlabcentral/fileexchange/40473-find_closest_row), MATLAB Central File Exchange. Retrieved .

Fan D.Chen

### Fan D.Chen (view profile)

Determines distance based on a Euclidean distance！

Jan

### Jan (view profile)

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.

Justin Winokur

### Justin Winokur (view profile)

@Jan Simon,

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

Jan

### Jan (view profile)

"(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)

##### MATLAB Release Compatibility
Created with R2010a
Compatible with any release
##### Platform Compatibility
Windows macOS Linux