intersect - Intersection of two sets of data - MATLAB (original) (raw)

Intersection of two sets of data

Syntax

Description

[C](#btcnv0o-1-C) = intersect([A,B](#btcnv0o-1%5Fsep%5Fshared-AB)) returns the data common to both A and B, with no repetitions. C is in sorted order.

example

[C](#btcnv0o-1-C) = intersect([A,B](#btcnv0o-1%5Fsep%5Fshared-AB),[setOrder](#btcnv0o-1-setOrder)) returns C in a specific order. setOrder can be 'sorted' or 'stable'.

example

[C](#btcnv0o-1-C) = intersect([A,B](#btcnv0o-1%5Fsep%5Fshared-AB),___,'rows') and`C` = intersect(`A,B`,'rows',___) treat each row of A and each row of B as single entities and return the rows common to both A andB, with no repetitions. You must specifyA and B and optionally can specifysetOrder.

The 'rows' option does not support cell arrays, unless one of the inputs is either a categorical array or a datetime array.

[[C](#btcnv0o-1-C),[ia](#btcnv0o-1-ia),[ib](#btcnv0o-1-ib)] = intersect(___) also returns index vectors ia and ib using any of the previous syntaxes.

example

[[C](#btcnv0o-1-C),[ia](#btcnv0o-1-ia),[ib](#btcnv0o-1-ib)] = intersect([A,B](#btcnv0o-1%5Fsep%5Fshared-AB),'legacy') and[`C`,`ia`,`ib`] = intersect(`A,B`,'rows','legacy') preserve the behavior of the intersect function from R2012b and prior releases.

The 'legacy' option does not support categorical arrays, datetime arrays, duration arrays, tables, or timetables.

example

Examples

collapse all

Intersection of Two Vectors

Create two vectors that have some values in common.

A = [7 1 7 7 4]; B = [7 0 4 4 0];

Find the values common to both A and B.

Intersection of Two Tables

Create two tables with rows in common.

A = table([1:5]',categorical({'A';'B';'C';'D';'E'}),logical([0;1;0;1;0]))

A=5×3 table Var1 Var2 Var3 ____ ____ _____

 1       A      false
 2       B      true 
 3       C      false
 4       D      true 
 5       E      false

B = table([1:2:10]',categorical({'A';'C';'E';'G';'I'}),logical(zeros(5,1)))

B=5×3 table Var1 Var2 Var3 ____ ____ _____

 1       A      false
 3       C      false
 5       E      false
 7       G      false
 9       I      false

Find the rows common to both A and B.

C=3×3 table Var1 Var2 Var3 ____ ____ _____

 1       A      false
 3       C      false
 5       E      false

Intersection of Two Vectors and Their Indices

Create two vectors with values in common.

A = [7 1 7 7 4]; B = [7 0 4 4 0];

Find the values common to both A and B, as well as the index vectors ia and ib, such that C = A(ia) and C = B(ib).

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

Intersection of Two Tables and Their Indices

Create a table, A, of gender, age, and height for five people.

A = table(categorical({'M';'M';'F';'M';'F'}),... [27;52;31;46;35],[74;68;64;61;64],... 'VariableNames',{'Gender' 'Age' 'Height'},... 'RowNames',{'Ted' 'Fred' 'Betty' 'Bob' 'Judy'})

A=5×3 table Gender Age Height ______ ___ ______

Ted        M       27       74  
Fred       M       52       68  
Betty      F       31       64  
Bob        M       46       61  
Judy       F       35       64  

Create a table, B, with rows in common with A.

B = table(categorical({'F';'M';'F';'F'}),... [31;47;35;23],[64;68;62;58],... 'VariableNames',{'Gender' 'Age' 'Height'},... 'RowNames',{'Meg' 'Joe' 'Beth' 'Amy'})

B=4×3 table Gender Age Height ______ ___ ______

Meg       F       31       64  
Joe       M       47       68  
Beth      F       35       62  
Amy       F       23       58  

Find the rows common to both A and B, as well as the index vectors ia and ib, such that C = A(ia,:) and C = B(ib,:).

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

C=1×3 table Gender Age Height ______ ___ ______

Betty      F       31       64  

Two rows that have the same values, but different names, are considered equal. Therefore, we discover that Betty, A(3,:), and Meg, B(1,:) have the same gender, age, and height.

Intersection of Rows in Two Matrices

Create two matrices with rows in common.

A = [2 2 2; 0 0 1; 1 2 3; 1 1 1]; B = [1 2 3; 2 2 2; 2 2 0];

Find the rows common to both A and B as well as the index vectors ia and ib, such that C = A(ia,:) and C = B(ib,:).

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

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')

Alternatively, you can specify 'sorted' order.

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

Intersection of Vectors Containing NaNs

Create two vectors containing NaN.

A = [5 NaN NaN]; B = [5 NaN NaN];

Find the values common to both A and B.

intersect treats NaN values as distinct.

Cell Array of Character Vectors with Trailing White Space

Create a cell array of character vectors, A.

A = {'dog','cat','fish','horse'};

Create a cell array of character vectors, B, where some of the vectors have trailing white space.

B = {'dog ','cat','fish ','horse'};

Find the character vectors common to both A and B.

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

C = 1x2 cell {'cat'} {'horse'}

intersect treats trailing white space in cell arrays of character vectors as distinct characters.

Intersection of Arrays of Different Classes and Shapes

Create a column vector character array.

A = ['A';'B';'C'], class(A)

A = 3x1 char array 'A' 'B' 'C'

Create a 2-by-3 matrix containing elements of numeric type double.

B = [65 66 67;68 69 70], class(B)

B = 2×3

65    66    67
68    69    70

Find the values common to both A and B.

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

C = 3x1 char array 'A' 'B' 'C'

intersect interprets B as a character array and returns a character array, C.

Intersection of Char and Cell Array of Character Vectors

Create a character vector containing animal names that have three letters.

A = ['dog';'cat';'fox';'pig']; class(A)

Create a cell array of character vectors containing animal names of varying lengths.

B = {'cat','dog','fish','horse'}; class(B)

Find the character vectors common to both A and B.

C = 2x1 cell {'cat'} {'dog'}

The result, C, is a cell array of character vectors.

Preserve Legacy Behavior of intersect

Use the 'legacy' flag to preserve the behavior of intersect from R2012b and prior releases in your code.

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

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

Find the unique elements of A and preserve the legacy behavior.

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

Input Arguments

collapse all

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

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

There are additional requirements for A and B based on data type:

A and B also can be objects with the following class methods:

The object class methods must be consistent with each other. These objects include heterogeneous arrays derived from the same root class. For example, A and B can be arrays of handles to graphics objects.

setOrder — Order flag

'sorted' (default) | 'stable'

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

Flag Description
'sorted' The values (or rows) in C return in sorted order as returned by sort.ExampleC = intersect([7 0 1 5],[0 2 7 5],'sorted')C = 0 5 7
'stable' The values (or rows) in C return in the same order as they appear inA.ExampleC = intersect([7 0 1 5],[0 2 7 5],'stable')C = 7 0 5

Data Types: char | string

Output Arguments

collapse all

C — Data common to A and B

vector | matrix | table | timetable

Data common to A and B, returned as a vector, matrix, or table. If the inputs A andB are tables or timetables, then the order of the variables in C is the same as the order of the variables in A.

The following describes the shape of C when the inputs are vectors or matrices and when the 'legacy' flag is not specified:

The class of the inputs A and B determines the class of C:

ia — Index to A

column vector

Index to A, returned as a column vector when the'legacy' flag is not specified. ia identifies the values (or rows) in A that are common toB. If there is a repeated value (or row) inA, then ia contains the index to the first occurrence of the value (or row).

ib — Index to B

column vector

Index to B, returned as a column vector when the'legacy' flag is not specified. ib identifies the values (or rows) in B that are common toA. If there is a repeated value (or row) inB, then ib contains the index to the first occurrence of the value (or row).

Tips

Extended Capabilities

Tall Arrays

Calculate with arrays that have more rows than fit in memory.

Theintersect function supports tall arrays with the following usage notes and limitations:

For more information, see Tall Arrays.

C/C++ Code Generation

Generate C and C++ code using MATLAB® Coder™.

Usage notes and limitations:

Thread-Based Environment

Run code in the background using MATLAB® backgroundPool or accelerate code with Parallel Computing Toolbox™ ThreadPool.

GPU Arrays

Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.

The intersect function supports GPU array input with these usage notes and limitations:

For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).

Distributed Arrays

Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.

Usage notes and limitations:

For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).

Version History

Introduced before R2006a