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
Equal to a(x)==z and b(y)==z.

Examples

Simple intersect example:

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

Simple intersect example with multiple return:

[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