table.union
table: tbl = union (tblA, tblB)
table: tbl = union (tblA, tblB, setOrder)
table: [tbl, ixA, ixB] = union (…)
Union of two tables by rows.
tbl = union (tblA, tblB) returns the combined
set of rows of 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 = union (tblA, tblB, setOrder)
controls the ordering of tbl. setOrder is either
'sorted' (default) for ascending order, or 'stable' to
keep the order in which the rows appear in tblA and tblB.
[tbl, ixA, ixB] = union (…) also returns
the index vectors ixA and ixB such that tbl is the
vertical concatenation of tblA(ixA,:) and
tblB(ixB,:).
Source Code: table
union returns the rows present in either table, with duplicates removed — set arithmetic over whole rows, comparing all variables together. Both tables must share the same variables; here each row spans three 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
union (A, B)
ans =
4x3 table
Name Age State
_________ ___ _____
{'Brown'} 49 MA
{'Diaz' } 40 CA
{'Lee' } 30 TX
{'Li' } 38 NY
Diaz and Brown match on every variable, so they appear once; the result has four distinct rows out of the six inputs.