setdiff

Returns the subset of the first set that does not belong to the second set.

Syntax

setdiff(a,b)

setdiff(a,b,'sorted')

setdiff(a,b,'stable')

setdiff(a,b,'rows')

[c,ia] = setdiff(...)

Inputs

a,b
The sets whose difference is to be formed.
Type: double | integer | char | string | logical | struct | cell
Dimension: scalar | string | vector | matrix
'sorted' (Default)
Returns elements of the difference set in ascending order.
Type: string
'stable'
Returns elements of the difference set in their original order.
Type: string
'rows'
Returns the difference set of rows as opposed to the elements.
Type: double | integer | char | string | logical | struct | cell
Dimension: scalar | string | vector | matrix

Outputs

c
The subset of a not belonging to b.
ia
The index vector such that a(ia) = c.

Examples

Single return case:
setdiff([1,5,2;6,2,6],[9;0;1;2])
ans = [Matrix] 2 x 1
5
6
Multi-return case:
[c,ia]=setdiff([1,5,2;6,2,6],[9;0;1;2])
c = [Matrix] 2 x 1
5
6
ia = [Matrix] 2 x 1
3
6
Single return case: with 'rows' option:
setdiff([1,5,2;6,2,6],[1 2 6],'rows')
ans = [Matrix] 2 x 3
1  5  2
6  2  6

Comments

When repeated elements occur in an input, the last index will be used.