setdiff

Elements of one set that are not in another.

Category: Language Fundamentals > Data Types

Syntax

C = setdiff(A, B)

Values in A not in B, without repeats, sorted.

C = setdiff(A, B, 'stable')

Keeps the order in which values first appear in A.

[C, ia] = setdiff(___)

Also returns the indices such that C = A(ia).

C = setdiff(T1, T2)

For tables, compares whole rows.

Description

The result never contains duplicates. By default it is sorted; use 'stable' to keep the original order. setdiff(A, B) is not symmetric: use setxor for the values that appear in only one of two sets.

Input arguments

A, B
Numeric arrays, string arrays, cell arrays of character vectors or tables.

Output arguments

C
Vector of the result. Row vector if A is a row, otherwise column.
ia
Column vector of indices into A.

Example

setdiff([5 1 3 3], [1 2])
setdiff([5 1 3], 1, 'stable')
setdiff({'a','b'}, {'b'})
Output
ans = 1×2

     3     5
ans = 1×2

     5     3
ans = 1×1 cell

    {'a'}

See also

union intersect setxor unique ismember