Code covered by the BSD License  

Highlights from
find_closest_row

Be the first to rate this file! 3 Downloads (last 30 days) File Size: 1.59 KB File ID: #40473

find_closest_row

by Justin Winokur

 

22 Feb 2013

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

| Watch this File

File Information
Description

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

MATLAB release MATLAB 7.10 (R2010a)
Tags for This File  
Everyone's Tags
data exploration, measurement, tools
Tags I've Applied
Add New Tags Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (3)
26 Feb 2013 Jan Simon

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.

23 Feb 2013 Justin Winokur

@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

23 Feb 2013 Jan Simon

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

Contact us