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.
Thanks - will implement your suggested improvements.
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: http://www.mathworks.com/matlabcentral/fileexchange/23294
Implemented some of Jan Simon's suggested improvements
Fixed a problem with sorting row vectors that caused the script to crash
Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.