table

Methods

Method Reference: table.intersect

table: tbl = intersect (tblA, tblB)
table: tbl = intersect (tblA, tblB, setOrder)
table: [tbl, ixA, ixB] = intersect (…)

Intersection of two tables by rows.

tbl = intersect (tblA, tblB) returns the set of rows common to both tblA and tblB, with duplicate rows removed. Both tables must have the same variable names, although not necessarily in the same order; tbl keeps the variable order of tblA. Rows are compared by their variable values only (row names are ignored), and by default tbl is sorted by those values.

tbl = intersect (tblA, tblB, setOrder) controls the ordering of tbl, either 'sorted' (default) or 'stable'.

[tbl, ixA, ixB] = intersect (…) also returns index vectors ixA and ixB such that tbl equals tblA(ixA,:) and tblB(ixB,:).

Source Code: table

Example: 1

intersect returns the rows common to both tables — the set intersection. A row matches only when every variable agrees, so mixed-type rows are compared field by field.

 A = table ({'Li'; 'Diaz'; 'Brown'}, [38; 40; 49], ...
            categorical ({'NY'; 'CA'; 'MA'}), ...
            'VariableNames', {'Name', 'Age', 'State'})
A =
  3x3 table

      Name       Age    State    
    _________    ___    _____    

    {'Li'   }     38       NY    
    {'Diaz' }     40       CA    
    {'Brown'}     49       MA
 B = table ({'Diaz'; 'Brown'; 'Lee'}, [40; 49; 30], ...
            categorical ({'CA'; 'MA'; 'TX'}), ...
            'VariableNames', {'Name', 'Age', 'State'})
B =
  3x3 table

      Name       Age    State    
    _________    ___    _____    

    {'Diaz' }     40       CA    
    {'Brown'}     49       MA    
    {'Lee'  }     30       TX
 intersect (A, B)
ans =
  2x3 table

      Name       Age    State    
    _________    ___    _____    

    {'Brown'}     49       MA    
    {'Diaz' }     40       CA

The extra outputs locate the shared rows within each input.

 [C, iA, iB] = intersect (A, B);
 [iA, iB]
ans =

   3   2
   2   1