View License

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

» Watch video

Highlights from

4.0 | 1 rating Rate this file 2 Downloads (last 30 days) File Size: 2.08 KB File ID: #27298 Version: 1.2



Vinesh Rajpaul (view profile)


19 Apr 2010 (Updated )

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

| Watch this File

File Information

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.

MATLAB release MATLAB 7.1.0 (R14SP3)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (2)
09 Jul 2010 Vinesh Rajpaul

Vinesh Rajpaul (view profile)

Thanks - will implement your suggested improvements.

Comment only
23 Jun 2010 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:

21 Jun 2010 1.1

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

22 Jul 2010 1.2

Implemented some of Jan Simon's suggested improvements

Contact us