File Exchange

image thumbnail


version 1.2 (2.08 KB) by

Finds elements in an array that approximate a given value, to within a specified error tolerance.



View License

Finding elements in an array that approximate a given value, to within a specified error tolerance, is a common task. This very simple script automates this task and furthermore outputs the relevant elemental indices in order of smallest to largest error, making it easy to select only the n closest matches.
Either a relative, percent or absolute error tolerance may be specified. Numeric arrays of all dimensions are supported.

Comments and Ratings (2)

Vinesh Rajpaul

Thanks - will implement your suggested improvements.

Jan Simon

Jan Simon (view profile)

This is a strange method to sort the replied indices:
I = sortrows([I, epsilon(I)], 2) * [1; 0]
This would be more efficient:
[dummy, Index] = sort(epsilon(I));
I = I(Index);
Applying relative tolerances to small values is a general problem. Perhaps it would be nicer to catch Value==0 for "relative" error directly, but at least a div-by-zero warning appears automatically.
I'd expect that Tol=0 performs an exact comparison, although this is nonsense for floating point calculations.
Descriptive help section, no standard H1-line, "See also", author and version, example, number of inputs checked (the MsgID starting with "MATLAB" looks like findTol belongs to a Matlab toolbox). Summary: Useful and usable, small improvements possible.
See also Bruno's ISMEMBERF:



Implemented some of Jan Simon's suggested improvements


Fixed a problem with sorting row vectors that caused the script to crash

MATLAB Release
MATLAB 7.1.0 (R14SP3)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Win prizes and improve your MATLAB skills

Play today