table

Methods

Method Reference: table.setdiff

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

Difference between two tables by rows.

tbl = setdiff (tblA, tblB) returns the set of rows that are present in tblA but not in 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 = setdiff (tblA, tblB, setOrder) controls the ordering of tbl, either 'sorted' (default) or 'stable'.

[tbl, ixA] = setdiff (…) also returns the index vector ixA such that tbl equals tblA(ixA,:).

Source Code: table

Example: 1

setdiff returns the rows that are in the first table but not the second, comparing whole multi-variable rows across all their types.

 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
 setdiff (A, B)
ans =
  1x3 table

     Name     Age    State    
    ______    ___    _____    

    {'Li'}     38       NY

Only Li is unique to A; a second output gives its row index in A.

 [C, iA] = setdiff (A, B);
 iA
iA = 1