union

Returns the elements that are in matrices x and y. If 'rows' is provided then each row will be considered its on set.

Syntax

union(x, y)

union(x, y, 'rows')

[c,ia,ib] = union(...)

Inputs

x,y
The sets to find a union of.
Any valid scalar | vector | matrix
Type: double | integer | char | string | logical | struct | cell
Dimension: scalar | vector | matrix
'rows'
Specifies to search for common rows as opposed to common elements.

Outputs

c
The elements of the union of x and y.
ia,ib
a(ia) and b(ib) are disjoint sets that compose c.

Examples

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