Note: This page has been translated by MathWorks. Click here to see

To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Set union of two arrays

`C = union(A,B)`

```
C
= union(A,B,setOrder)
```

`C = union(A,B,___,'rows')`

`C = union(A,B,'rows',___)`

```
[C,ia,ib]
= union(___)
```

```
[C,ia,ib]
= union(A,B,'legacy')
```

```
[C,ia,ib]
= union(A,B,'rows','legacy')
```

returns the combined data from `C`

= union(`A,B`

)`A`

and `B`

with no repetitions. `C`

is in sorted order.

If

`A`

and`B`

are tables or timetables, then`union`

returns the combined set of rows from both tables. For timetables,`union`

takes row times into account to determine equality, and sorts the output timetable`C`

by row times.

and
`C`

= union(`A,B`

,___,'rows')

treat each row of `C`

= union(`A,B`

,'rows',___)`A`

and each row of `B`

as
single entities and return the combined rows from `A`

and
`B`

, with no repetitions. You must specify
`A`

and `B`

and optionally can specify
`setOrder`

.

The `'rows'`

option does not support cell arrays, unless one
of the inputs is either a categorical array or a datetime array.

`[`

also returns index vectors `C`

,`ia`

,`ib`

]
= union(___)`ia`

and `ib`

using
any of the previous syntaxes.

Generally, the values in

`C`

are a sorted combination of the elements of`A(ia)`

and`B(ib)`

.If the

`'rows'`

option is specified, then the rows of`C`

are a sorted combination of the rows of`A(ia,:)`

and`B(ib,:)`

.If

`A`

and`B`

are tables or timetables, then`C`

is a sorted combination of the rows of`A(ia,:)`

and`B(ib,:)`

.

To find the union with respect to a subset of variables from a table or timetable, you can use column subscripting. For example, you can use

`union(A(:,`

, where),B(:,`vars`

))`vars`

is a positive integer, a vector of positive integers, a variable name, a cell array of variable names, or a logical vector. Alternatively, you can use`vars`

`vartype`

to create a subscript that selects variables of a specified type.

Was this topic helpful?