C = union(A,B) returns
the combined data from A and B with
no repetitions.

If A and B are
numeric arrays, logical arrays, character arrays, categorical arrays,
or cell arrays of strings, then union returns
the combined values from A and B.
The values of C are in sorted order.

If A and B are
tables, then union returns the combined set of
rows from both tables. The rows of table C are
in sorted order.

C = union(A,B,'rows') treats
each row of A and each row of B as
single entities and returns the combined rows from A and B with
no repetitions. The rows of C are in sorted order.

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

If A and B are
numeric arrays, logical arrays, character arrays, categorical arrays,
or cell arrays of strings, then the values in C are
a sorted combination of the values of A(ia) and B(ib).

If A and B are
tables, then C is a sorted combination of the rows
of A(ia,:) and B(ib,:).

[C,ia,ib]
= union(A,B,'rows') also returns index
vectors ia and ib, such that
the rows of C are a sorted combination of the rows
of A(ia,:) and B(ib,:).

[C,ia,ib]
= union(___,setOrder) returns C in
a specific order using any of the input arguments in the previous
syntaxes. setOrder='sorted' returns the values
(or rows) of C in sorted order. setOrder='stable' returns
the values (or rows) of C in the same order as A,
and then B. If no value is specified, the default
is 'sorted'.

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

A =
Var1 Var2 Var3
---- ---- -----
1 A false
2 B true
3 C false
4 D true
5 E false
B =
Var1 Var2 Var3
---- ---- -----
1 A false
3 C false
5 E false
7 G false
9 I false

Find the union of tables A and B.

C = union(A,B)

C =
Var1 Var2 Var3
---- ---- -----
1 A false
2 B true
3 C false
4 D true
5 E false
7 G false
9 I false

Input arrays, specified as numeric arrays, logical arrays, character
arrays, categorical arrays, cell arrays of strings, or tables.

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 strings can combine with char arrays.

Categorical arrays can combine with cell arrays of
strings or single strings.

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 you specify the 'rows' option, A and B must
have the same number of columns.

If A and B are tables,
they must have the same variable names. Conversely, the row names
do not matter. Two rows that have the same values, but different names,
are considered equal.

Furthermore, A and B 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.

Combined data of A and B,
returned as a vector, matrix, or table. If the inputs A and B are
tables, the order of the variables in the resulting table, C,
is the same as the order of the variables in A.

The following describes the shape of C when
the inputs are vector 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.

If the 'rows' flag is not specified
and both A and B are row vectors,
then C is a row vector.

If the 'rows' flag is specified,
then C is a matrix containing the combined rows
of 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 strings with char,
then C is a cell array of strings.

If you combine a categorical array with a cell array
of strings or single string, then C is a categorical
array.

Index to A, returned as a column vector when
the 'legacy' flag is not specified. ia indicates
the values (or rows) in A that contribute to the
union. If a value (or row) appears multiple times in A,
then ia contains the index to the first occurrence
of the value (or row). If a value appears in both A and B,
then ia contains the index to the first occurrence
in A.

Index to B, returned as a column vector when
the 'legacy' flag is not specified. ib indicates
the values (or rows) in B that contribute to the
union. If there is a repeated value (or row) appearing exclusively
in B, then ib contains the index
to the first occurrence of the value. If a value (or row) appears
in both A and B, then ib does
not contain an index to the value (or row).

To find the union with respect to a subset of variables
from a table, you can use column subscripting. For example, you can
use union(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.