Skip to Main Content Skip to Search
Product Documentation

union - Find set union of two arrays

In a future release, the behavior of union will change. This change is introduced for adoption in R2012a. For a detailed demonstration that shows how you can preview the future behavior and preserve the current behavior of your existing code, see the example, Assessing the Impact of the Forthcoming Changes to union. For a detailed explanation of all the forthcoming changes, see Set Functions Changing Behavior in a Future Release.

Syntax

  • C = union(A,B) example
  • C = union(A,B,'rows')
  • [C,ia,ib] = union(A,B) example
  • [C,ia,ib] = union(A,B,'rows') example

  • [C,ia,ib] = union(___,'R2012a') example
  • [C,ia,ib] = union(___,'legacy') example

  • [C,ia,ib] = union(A,B,setOrder) example
  • [C,ia,ib] = union(A,B,'rows',setOrder) example

Description

example

C = union(A,B) returns the combined values from A and B with no repetitions. The values of C are in sorted order.

C = union(A,B,'rows') returns the combined rows from A and B with no repetitions. The rows of the matrix C are in sorted order.

example

[C,ia,ib] = union(A,B) also returns index vectors ia and ib, such that the values in C are the combined values of A(ia) and B(ib).

example

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

example

[C,ia,ib] = union(___,'R2012a') adopts the future behavior of the union function. [C,ia,ib] = union(___,'legacy') preserves the current behavior. The ___ symbol signifies that you can specify either flag, 'R2012a' or 'legacy', as the final argument with any previous syntax that accepts A, B, or 'rows'.

example

[C,ia,ib] = union(A,B,setOrder) and [C,ia,ib] = union(A,B,'rows',setOrder) returns 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.

Examples

Union of Two Vectors

A = [5 7 1]; B = [3 1 1];
C = union(A,B)
C =

     1     3     5     7

Find the Union of Two Vectors and Their Indices

A = [5 7 1]; B = [3 1 1];
[C,ia,ib] = union(A,B)
C =

     1     3     5     7


ia =

     1     2


ib =

     3     1

Union of the Rows in Two Matrices

A = [2 2 2; 0 0 1];
B = [1 2 3; 2 2 2; 2 2 2];
[C,ia,ib] = union(A,B,'rows')
C =

     0     0     1
     1     2     3
     2     2     2


ia =

     2


ib =

     1
     3

Union of Two Vectors with Specified Output Order

Use the setOrder argument to specify the ordering of the values in C.

Specify 'stable' if you want the values in C to have the same order as in A and B.

A = [5 7 1]; B = [3 1 1];
[C,ia,ib] = union(A,B,'stable')
C =

     5     7     1     3


ia =

     1
     2
     3


ib =

     1

Alternatively, you can specify 'sorted' order.

A = [5 7 1]; B = [3 1 1];
[C,ia,ib] = union(A,B,'sorted')
C =

     1     3     5     7


ia =

     3
     1
     2


ib =

     1

union adopts the forthcoming behavior when you specify the setOrder argument. If you omit the setOrder argument, the output is sorted, but the size and content of C, ia, and ib conforms to the current behavior. Compare the sorted output here to the output from the example, Find the Union of Two Vectors and Their Indices.

Assessing the Impact of the Forthcoming Changes to union

Use the 'R2012a' flag to assess the impact of the forthcoming behavior changes. Use the 'legacy' flag to preserve the current behavior of your existing code.

Find the union of A and B with the current default behavior.

A = [5 7 1]; B = [3 1 1];
[C1,ia1,ib1] = union(A,B)
C1 =

     1     3     5     7


ia1 =

     1     2


ib1 =

     3     1

Find the union of A and B, and opt into the forthcoming behavior. In the future, this behavior will be the default.

A = [5 7 1]; B = [3 1 1];
[C2,ia2,ib2] = union(A,B,'R2012a')
C2 =

     1     3     5     7


ia2 =

     3
     1
     2


ib2 =

     1

Notice that ia1 and ia2 have different shapes and content. The same is also true for ib1 and ib2.

Find the union of A and B, and preserve the current behavior.

A = [5 7 1]; B = [3 1 1];
[C3,ia3,ib3] = union(A,B,'legacy')
C3 =

     1     3     5     7


ia3 =

     1     2


ib3 =

     3     1

C3, ia3, and ib3 match C1, ia1, and ib1, respectively.

Union of Vectors Containing NaNs

A = [5 NaN 1]; B = [4 NaN NaN];
C = union(A,B)
C =

     1     4     5   NaN   NaN   NaN

union treats NaN values as distinct.

Input Arguments

A,B

Input Arrays
Vectors | Matrices | N-D Arrays

Input arrays of which to find the union. A and B can be logical, char, a cell array of strings, or any numeric class. A and B also can be any object with the class methods: sort (or sortrows for the 'rows' option), and ne. This includes heterogeneous arrays derived from the same root class.

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.

If you specify the 'rows' option, A and B must have the same number of columns.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical
Complex Support: Yes

'rows'

Rows Flag

Rows flag, specified as 'rows', identifies the rows of A and B as individual elements. When you specify this flag, union returns the combined rows of A and B. This option does not support cell arrays.

setOrder

Order Flag
'sorted' | 'stable'

Order flag, specified as 'sorted' or 'stable', indicates the order of the values (or rows) in C.

Order FlagMeaning
'sorted'The values (or rows) in C, are returned in sorted order. For example: C = union([5 5 3],[1 2],'sorted') returns C = [1 2 3 5].
'stable'The values (or rows) in C, are returned in the same order as they appear in A and B. For example: C = union([5 5 3],[1 2],'stable') returns C = [5 3 1 2].

'legacy'

Legacy Behavior Flag

Legacy behavior flag, when specified, preserves the current behavior. If the new behavior adversely affects your existing code, you can preserve the current behavior by specifying 'legacy' as the final argument.

'R2012a'

Future Behavior Flag

Future behavior flag, when specified, adopts the future behavior change. Specify R2012a' as the final argument to see what impact the new behavior will have on your existing code.

Output Arguments

C

Combined Values of A and B
Vector | Matrix

Combined values of A and B returned as a vector or matrix. C is a vector unless you specify the 'rows' flag. If 'rows' is specified, C is a matrix containing the combined rows of A and B.

ia

Index to A
Vector

Index to A is a vector that indicates the values (or rows) in A that contribute to the union. If there is a repeated value (or row) appearing exclusively in A, then ia contains the index to the last occurrence of the value (or row).

If you call union with the setOrder or 'R2012a' argument, ia contains the index to the first occurrence of any repeated value (or row) in A.

ib

Index to B
Vector

Index to B is a vector that indicates the values (or rows) in B that contribute to the union. If a value (or row) appears multiple times in B, then ib contains the index to the last occurrence of the value (or row). If a value (or row) appears in both A and B, then ib contains the index to the last occurrence in B.

If you call union with the setOrder or 'R2012a' argument, ib contains the index to the first occurrence of any repeated values (or rows).

See Also

intersect | ismember | issorted | setdiff | setxor | sort | unique

  


» Learn more
» Download free kit
» Get trial software

 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS