Documentation

This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.

라이선스가 부여된 사용자만 번역 문서를 볼 수 있습니다. 번역 문서를 보려면 로그인하십시오.

intersect

Set intersection of two arrays

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(___,setOrder)
    example
  • [C,ia,ib] = intersect(A,B,'legacy')
    example
  • [C,ia,ib] = intersect(A,B,'rows','legacy')
    example

Description

example

C = intersect(A,B) returns the data common to both A and B, with no repetitions. C is in sorted order.

  • If A and B are tables or timetables, then intersect returns the set of rows common to both tables. For timetables, intersect takes row times into account to determine equality, and sorts the output timetable C by row times.

C = intersect(A,B,'rows') treats each row of A and each row of B as single entities and returns the rows common to both A and B, with no repetitions. C is in sorted order.

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

example

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

  • Generally, C = A(ia) and C = B(ib).

  • If A and B are tables or timetables, then 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(___,setOrder) returns C in a specific order using any of the input arguments in the previous syntaxes. setOrder can be 'sorted' or 'stable':

  • 'sorted'C is in sorted order. This is the default.

  • 'stable'C is in the same order as A.

example

[C,ia,ib] = intersect(A,B,'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.

Examples

collapse all

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.

C = intersect(A,B)
C =

     4     7

Create two tables with rows in common.

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

    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 = 

    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 = intersect(A,B)
C = 

    Var1    Var2    Var3 
    ____    ____    _____

    1       A       false
    3       C       false
    5       E       false

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)
C =

     4     7


ia =

     5
     1


ib =

     3
     1

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 = 

             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 = 

            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 = 

             Gender    Age    Height
             ______    ___    ______

    Betty    F         31     64    


ia =

     3


ib =

     1

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.

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

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

Create two vectors containing NaN.

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

Find the values common to both A and B.

C = intersect(A,B)
C =

     5

intersect treats NaN values as distinct.

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 =

  1×2 cell array

    'cat'    'horse'


ia =

     2
     4


ib =

     2
     4

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

Create a column vector character array.

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

A
B
C


ans =

char

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

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

    65    66    67
    68    69    70


ans =

double

Find the values common to both A and B.

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

A
B
C


ia =

     1
     2
     3


ib =

     1
     3
     5

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

class(C)
ans =

char

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

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

char

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

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

cell

Find the character vectors common to both A and B.

C = intersect(A,B)
C =

  2×1 cell array

    'cat'
    'dog'

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

class(C)
ans =

cell

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)
C1 =

     4     7


ia1 =

     5
     1


ib1 =

     3
     1

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

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

     4     7


ia2 =

     5     4


ib2 =

     4     1

Input Arguments

collapse all

Input arrays, specified as numeric arrays, logical arrays, character arrays, string arrays, categorical arrays, datetime arrays, duration arrays, cell arrays of character vectors, tables, or timetables. If you specify the 'rows' option, A and B must have the same number of columns.

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 character vectors can combine with character arrays or string arrays.

  • Categorical arrays can combine with character arrays, cell arrays of character vectors, or string arrays.

  • Datetime arrays can combine with cell arrays of date character vectors or single date character vectors.

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

  • If A and B are both ordinal categorical arrays, they must have the same sets of categories, including their order. If neither A nor B are ordinal, they need not have the same sets of categories, and the comparison is performed using the category names. In this case, the categories of C are the sorted union of the categories from A and B.

  • If A and B are tables or timetables, they must have the same variable names (except for order). For tables, row names are ignored, so that two rows that have the same values, but different names, are considered equal. For timetables, row times are taken into account, so that two rows that have the same values, but different times, are not considered equal.

  • If A and B are datetime arrays, they must be consistent with each other in whether they specify a time zone.

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

  • sort (or sortrows for the 'rows' option)

  • ne

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.

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 return 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 return 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].

Output Arguments

collapse all

Data common to A and B, returned as a vector, matrix, or table. If the inputs A and B 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:

  • If the 'rows' flag is not specified, then C is a column vector unless both A and B are row vectors, in which case C is a row vector.

  • If the 'rows' flag is specified, then C is a matrix containing the rows in common from A and B.

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

  • If the class of A and B are the same, then C is the same class.

  • If you combine a char or nondouble numeric class with double, then C is the same class as the nondouble input.

  • If you combine a logical class with double, then C is double.

  • If you combine a cell array of character vectors with char, then C is a cell array of character vectors.

  • If you combine a categorical array with a character vector, cell array of character vectors, or string, then C is a categorical array.

  • If you combine a datetime array with a cell array of date character vectors or single date character vector, then C is a datetime array.

  • If you combine a string array with a character vector or cell array of character vectors, then C is a string array.

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 to B. If there is a repeated value (or row) in A, then ia contains the index to the first occurrence of the value (or row).

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 to A. If there is a repeated value (or row) in B, then ib contains the index to the first occurrence of the value (or row).

More About

collapse all

Tips

  • To find the intersection with respect to a subset of variables from a table or timetable, you can use column subscripting. For example, you can use intersect(A(:,vars),B(:,vars)), where vars is a positive integer, a vector of positive integers, a variable name, a cell array of variable names, or a logical vector. Alternatively, you can use vartype to create a subscript that selects variables of a specified type.

Introduced before R2006a

Was this topic helpful?