intersect

Returns the elements in both a and b.

Syntax

intersect(a,b)

intersect(a,b,'rows')

[x,y,z] = intersect(a,b)

Inputs

a,b
The sets to search for common elements in.
Type: double | integer | char | string | logical | struct | cell
Dimension: scalar | string | vector | matrix
'rows'
Specifies to search for common rows as opposed to common elements.
Type: double | integer | char | string | logical | struct | cell
Dimension: scalar | string | vector | matrix

Outputs

R
Common elements in ascending order.
x,y,z
x is the list of elements common to both sets (a and b).
y and z are the indices, for a and b, of the elements of x. They correspond to a(y)==xand b(z)==x.

Examples

Simple intersect example:

intersect([1,5,2;6,2,6],[9;0;1;2])
R = [ 1 ; 2 ]

Simple intersect example with multiple returns:

[a,b,c]=intersect([1,5,2;6,2,6],[9;0;1;2])
a = [ 1 ; 2 ]
b = [ 1 ; 5 ]
c = [ 3 ; 4 ]

Simple intersect example with rows option:

intersect([1,5,2;6,2,6],[9,0,1;6,2,6],'rows')
ans = [Matrix] 1 x 3
6  2  6