Accelerating the pace of engineering and science

# Documentation Center

• Trial Software

# union

Class: categorical

Set union for categorical arrays

The nominal and ordinal array data types might be removed in a future release. To represent ordered and unordered discrete, nonnumeric data, use the MATLAB® categorical data type instead.

## Syntax

C = union(A,B)
[C,IA,IB] = union(A,B)
[...] = union(A,B,'rows')
[...] = union(...,'legacy')
[...] = union(A,B,setOrder)
[...] = union(A,B,'rows',setOrder)

## Description

C = union(A,B) for categorical vectors A and B, returns a categorical vector C containing the combined values from A and B with no repetitions. The result C is sorted. The set of categorical levels for C is the sorted union of the sets of levels of the inputs, as determined by their labels.

[C,IA,IB] = union(A,B) also returns index vectors IA and IB such that C is a sorted combination of the elements A(IA) and B(IB). If there are common values in A and B, then the index is returned in IB. If there are repeated values in A or B, then the index of the last occurrence of each repeated value is returned.

[...] = union(A,B,'rows') for categorical matrices A and B with the same number of columns, returns the combined rows from the two matrices with no repetitions. The rows of the matrix C are sorted. The set of categorical levels for C is the sorted union of the sets of levels of the inputs. The optional outputs IA and IB are index vectors such that C is the sorted combination of the rows C = A(IA,:) and C = B(IB,:).

[...] = union(...,'legacy') preserves the pre-2012a behavior of union. You can specify the flag as the final argument with any previous syntax that accepts A, B, or 'rows'.

[...] = union(A,B,setOrder) and [...] = union(A,B,'rows',setOrder) returns the observations of C in a specific order. 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, then B. If A and B are row vectors, then C is also a row vector. Otherwise, C is a column vector. IA and IB are column vectors. If there are repeated common values in A or B, then the index of the first occurrence of each repeated value is returned.