ismembertol

Tests set membership of floating-point values within a tolerance.

Category: Language Fundamentals > Operators and Elementary Operations

Syntax

tf = ismembertol(a, S)

Returns true for each element of a that is within the default tolerance of some element of S.

tf = ismembertol(a, S, tol)

Uses the tolerance tol.

[tf, loc] = ismembertol(___)

Also returns the index in S of a matching element.

Description

Two numbers are considered members of each other when abs(u - v) <= tol * max(abs([a(:); S(:)])). Plain ismember demands exact equality, which fails for values such as 0.1 + 0.2 and 0.3 that differ in the last bits.

The tolerance is relative to the data, so it scales sensibly whether the numbers are tiny or huge.

Input arguments

a
Values to look up.
S
Set of values to search in.
tol
Relative tolerance, scaled by the largest absolute value in a and S. Defaults are 1e-6 for single and 1e-12 for double.

Output arguments

tf
Logical array the size of a.
loc
Index into S for each match, 0 where there is none.

Example

ismember(0.1 + 0.2, 0.3)
ismembertol(0.1 + 0.2, 0.3)
[tf, loc] = ismembertol([0.1 + 0.2, 5], [0.3 1 5])
Output
ans = logical
   0
ans = logical
   1
tf = 1×2 logical array

   1   1
loc = 1×2

     1     3

See also

ismember uniquetol abs eps