uniquetol

Unique values of a floating-point array within a tolerance.

Category: Language Fundamentals > Operators and Elementary Operations

Syntax

C = uniquetol(A)

Returns the sorted unique values, treating values within the default tolerance as equal.

C = uniquetol(A, tol)

Uses the tolerance tol.

[C, ia, ic] = uniquetol(___)

Also returns indices such that C = A(ia) and A is approximately C(ic).

Description

unique treats numbers as different if they differ in even the last bit, so results of floating-point computations such as 0.3 - 0.2 and 0.1 are kept apart. uniquetol groups values that differ by at most tol * max(abs(A)) and keeps one value from each group.

The output is always sorted and returned as a column or a row following the shape of A.

Input arguments

A
Numeric array.
tol
Relative tolerance, scaled by max(abs(A(:))). Default 1e-12 for double and 1e-6 for single.

Output arguments

C
Sorted vector of representatives, one per group of nearly equal values.
ia, ic
Index vectors relating A and C.

Example

a = [0.1 0.3-0.2 0.7 0.7000001];
numel(unique(a))
uniquetol(a, 1e-5)
Output
ans = 4
ans = 1×2

    0.1000    0.7000

See also

unique ismembertol sort abs