Skip to Main Content Skip to Search
Product Documentation

intersect - Find set intersection of two arrays

In a future release, the behavior of intersect will change. This change is introduced for adoption in R2012a. For a detailed demonstration that shows how you can preview the future behavior and preserve the current behavior of your existing code, see the example, Assessing the Impact of Forthcoming Changes to intersect. For a detailed explanation of all the forthcoming changes, see Set Functions Changing Behavior in a Future Release.

Syntax

  • C = intersect(A,B) example
  • C = intersect(A,B,'rows')
  • [C,ia,ib] = intersect(A,B) example
  • [C,ia,ib] = intersect(A,B,'rows') example

  • [C,ia,ib] = intersect(___,'R2012a') example
  • [C,ia,ib] = intersect(___,'legacy') example

  • [C,ia,ib] = intersect(A,B,setOrder) example
  • [C,ia,ib] = intersect(A,B,'rows',setOrder) example

Description

example

C = intersect(A,B) returns the values common to both A and B. The values of C are in sorted order.

C = intersect(A,B,'rows') returns the rows common to both A and B. The rows of the matrix C are in sorted order.

example

[C,ia,ib] = intersect(A,B) also returns index vectors ia and ib, such that C = A(ia) and C = B(ib).

example

[C,ia,ib] = intersect(A,B,'rows') also returns index vectors ia and ib, such that C = A(ia,:) and C = B(ib,:).

example

[C,ia,ib] = intersect(___,'R2012a') adopts the future behavior of the intersect function. [C,ia,ib] = intersect(___,'legacy') preserves the current behavior. The ___ symbol signifies that you can specify either flag, 'R2012a' or 'legacy', as the final argument with any previous syntax that accepts A, B, or 'rows'.

example

[C,ia,ib] = intersect(A,B,setOrder) and [C,ia,ib] = intersect(A,B,'rows',setOrder) return C in a specific order. setOrder='sorted' returns the values (or rows) of C in sorted order. setOrder='stable' returns the values (or rows) of C in the same order as A, then B.

Examples

Intersection of Two Vectors

A = [7 1 7 7 4]; B = [7 0 4 4 0];
C = intersect(A,B)
C =

     4     7

Find the Intersection of Two Vectors and Their Indices

A = [7 1 7 7 4]; B = [7 0 4 4 0];
[C,ia,ib] = intersect(A,B)
C =

     4     7


ia =

     5     4


ib =

     4     1

Intersection of Rows in Two Matrices

A = [2 2 2; 0 0 1; 1 2 3; 1 1 1];
B = [1 2 3; 2 2 2; 2 2 0];
[C,ia,ib] = intersect(A,B,'rows')
C =

     1     2     3
     2     2     2


ia =

     3
     1


ib =

     1
     2

A and B do not need to have the same number of rows, but they must have the same number of columns.

Intersection with Specified Output Order

Use the setOrder argument to specify the ordering of the values in C.

Specify 'stable' if you want the values in C to have the same order as in A.

A = [7 1 7 7 4]; B = [7 0 4 4 0];
[C,ia,ib] = intersect(A,B,'stable')
C =

     7     4


ia =

     1
     5


ib =

     1
     3

Alternatively, you can specify 'sorted' order.

[C,ia,ib] = intersect(A,B,'sorted')
C =

     4     7


ia =

     5
     1


ib =

     3
     1

intersect adopts the forthcoming behavior when you specify the setOrder argument. If you omit the setOrder argument, the output is sorted, but the size and content of C, ia, and ib conforms to the current behavior.

Assessing the Impact of Forthcoming Changes to intersect

Use the 'R2012a' flag to assess the impact of the forthcoming behavior changes. Use the 'legacy' flag to preserve the current behavior of your existing code.

Find the intersection of A and B with the current default behavior.

A = [7 1 7 7 4]; B = [7 0 4 4 0];
[C1,ia1,ib1] = intersect(A,B)
C1 =

     4     7


ia1 =

     5     4


ib1 =

     4     1

Find the intersection of A and B, and opt into the forthcoming behavior. In the future, this behavior will be the default.

[C2,ia2,ib2] = intersect(A,B,'R2012a')
C2 =

     4     7


ia2 =

     5
     1


ib2 =

     3
     1

Notice that ia1 and ia2 have different shapes and content. The same is also true for ib1 and ib2.

Find the unique elements of A, and preserve the current behavior.

[C3,ia3,ib3] = intersect(A,B,'legacy')
C3 =

     4     7


ia3 =

     5     4


ib3 =

     4     1

Notice that C3, ia3, and ib3 match C1, ia1, and ib1 respectively.

Intersection of Vectors Containing NaNs

A = [5 NaN NaN]; B = [5 NaN NaN];
C = intersect(A,B)
C =

     5

intersect treats NaN values as distinct.

Input Arguments

A,B

Input Arrays
Vectors | Matrices | N-D Arrays

Input arrays of which to find the intersection. A and B can be logical, char, a cell array of strings, or any numeric class. A and B also can be any object with the class methods: sort (or sortrows for the 'rows' option), eq, and ne. This includes heterogeneous arrays derived from the same root class.

A and B must be of the same class with the following exceptions:

  • logical, char, and all numeric classes can combine with double arrays.

  • Cell arrays of strings can combine with char arrays.

If you specify the 'rows' option, A and B must have the same number of columns.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical
Complex Support: Yes

'rows'

Rows Flag

Rows flag, specified as 'rows', identifies the rows of A and B as individual elements. When you specify this flag, intersect returns the rows common to both A and B. This option does not support cell arrays.

setOrder

Order Flag
'sorted' | 'stable'

Order flag, specified as 'sorted' or 'stable', indicates the order of the values (or rows) in C.

Order FlagMeaning
'sorted'The values (or rows) in C, are returned in sorted order. For example: C = intersect([7 0 5],[7 1 5],'sorted') returns C = [5 7].
'stable'The values (or rows) in C, are returned in the same order as they appear in A and B. For example: C = intersect([7 0 5],[7 1 5],'stable') returns C = [7 5].

'legacy'

Legacy Behavior Flag

Legacy behavior flag, when specified, preserves the current behavior. If the new behavior adversely affects your existing code, you can preserve the current behavior by specifying 'legacy' as the final argument.

'R2012a'

Future Behavior Flag

Future behavior flag, when specified, adopts the future behavior change. Specify R2012a' as the final argument to see what impact the new behavior has on your existing code.

Output Arguments

C

Values Common to A and B
Vector | Matrix

Values common to both A and B, returned as a vector or matrix. C is a vector unless you specify the 'rows' flag. If 'rows' is specified, C is a matrix containing the rows common to both A and B.

ia

Index to A
Vector

Index vector that identifies the elements in A that are common to B. If there is a repeated value (or row) in A, then ia contains the index to the last occurrence of the value (or row).

If you call intersect with the setOrder or 'R2012a' argument, ia contains the index to the first occurrence of any repeated value (or row) in A.

ib

Index to B
Vector

Index vector that identifies the elements in B that are common to A. If there is a repeated value (or row) in B, then ib contains the index to the last occurrence of the value (or row).

If you call intersect with the setOrder or 'R2012a' argument, ib contains the index to the first occurrence of any repeated value (or row) in B.

See Also

ismember | issorted | setdiff | setxor | sort | union | unique

  


Free MATLAB Interactive Kit

Explore how to use MATLAB to make advancements in engineering and science.


Download free kit

Trials Available

Try the latest version of MATLAB and other MathWorks products.


Get trial software
 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS