Main Content

union

Set union of two arrays

Description

example

C = union(A,B) returns the combined data from 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.

example

C = union(A,B,setOrder) returns C in a specific order. setOrder can be 'sorted' or 'stable'.

C = union(A,B,___,'rows') and C = union(A,B,'rows',___) treat each row of 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.

example

[C,ia,ib] = union(___) also returns index vectors 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,:).

example

[C,ia,ib] = union(A,B,'legacy') and [C,ia,ib] = union(A,B,'rows','legacy') preserve the behavior of the union function from R2012b and prior releases.

The 'legacy' option does not support categorical arrays, datetime arrays, duration arrays, tables, or timetables.

Examples

collapse all

Define two vectors with a value in common.

A = [5 7 1]; 
B = [3 1 1];

Find the union of vectors A and B.

C = union(A,B)
C = 1×4

     1     3     5     7

Define two tables with rows in common.

A = table([1:5]',['A';'B';'C';'D';'E'],logical([0;1;0;1;0]))
A=5×3 table
    Var1    Var2    Var3 
    ____    ____    _____

     1       A      false
     2       B      true 
     3       C      false
     4       D      true 
     5       E      false

B = table([1:2:10]',['A';'C';'E';'G';'I'],logical(zeros(5,1)))
B=5×3 table
    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=7×3 table
    Var1    Var2    Var3 
    ____    ____    _____

     1       A      false
     2       B      true 
     3       C      false
     4       D      true 
     5       E      false
     7       G      false
     9       I      false

Define two vectors with a value in common.

A = [5 7 1]; 
B = [3 1 1];

Find the union of vectors A and B, as well as the index vectors, ia and ib.

[C,ia,ib] = union(A,B)
C = 1×4

     1     3     5     7

ia = 3×1

     3
     1
     2

ib = 1

The values in C are the combined values of A(ia) and B(ib).

Define a table, A, of gender, age, and height for three people.

A = table(['M';'M';'F'],[27;52;31],[74;68;64],...
'VariableNames',{'Gender' 'Age' 'Height'},...
'RowNames',{'Ted' 'Fred' 'Betty'})
A=3×3 table
             Gender    Age    Height
             ______    ___    ______

    Ted        M       27       74  
    Fred       M       52       68  
    Betty      F       31       64  

Define a table, B with the same variables as A.

B = table(['F';'M'],[64;68],[31;47],...
'VariableNames',{'Gender' 'Height' 'Age'},...
'RowNames',{'Meg' 'Joe'})
B=2×3 table
           Gender    Height    Age
           ______    ______    ___

    Meg      F         64      31 
    Joe      M         68      47 

Find the union of tables A and B, as well as the index vectors, ia and ib.

[C,ia,ib] = union(A,B)
C=4×3 table
             Gender    Age    Height
             ______    ___    ______

    Betty      F       31       64  
    Ted        M       27       74  
    Joe        M       47       68  
    Fred       M       52       68  

ia = 3×1

     3
     1
     2

ib = 2

The data for Meg and Betty are the same. union only returns the index from A, which corresponds to Betty.

Define two matrices with a row in common.

A = [2 2 2; 0 0 1];
B = [1 2 3; 2 2 2; 2 2 2];

Find the combined rows of A and B, with no repetition, as well as the index vectors ia and ib.

[C,ia,ib] = union(A,B,'rows')
C = 3×3

     0     0     1
     1     2     3
     2     2     2

ia = 2×1

     2
     1

ib = 1

The rows of C are the combined rows of A(ia,:) and B(ib,:).

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 = 1×4

     5     7     1     3

ia = 3×1

     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×4

     1     3     5     7

ia = 3×1

     3
     1
     2

ib = 1

Define two vectors containing NaN.

A = [5 NaN 1]; 
B = [4 NaN NaN];

Find the union of vectors A and B.

C = union(A,B)
C = 1×6

     1     4     5   NaN   NaN   NaN

union treats NaN values as distinct.

Create a cell array of character vectors, A.

A = {'dog','cat','fish','horse'};

Create a cell array of character vectors, B, where some of the vectors have trailing white space.

B = {'dog ','cat','fish ','horse'};

Combine the elements of A and B.

[C,ia,ib] = union(A,B)
C = 1x6 cell
    {'cat'}    {'dog'}    {'dog '}    {'fish'}    {'fish '}    {'horse'}

ia = 4×1

     2
     1
     3
     4

ib = 2×1

     1
     3

union treats trailing white space in cell arrays of character vectors as distinct characters.

Create a column vector character array.

A = ['A';'B';'C']
A = 3x1 char array
    'A'
    'B'
    'C'

class(A)
ans = 
'char'

Create a row vector containing elements of numeric type double.

B = [68 69 70]
B = 1×3

    68    69    70

class(B)
ans = 
'double'

The union of A and B returns a column vector character array.

C = union(A,B)
C = 6x1 char array
    'A'
    'B'
    'C'
    'D'
    'E'
    'F'

class(C)
ans = 
'char'

Create a character vector containing the letters a , b, and c.

A = ['a';'b';'c'];
class(A)
ans = 
'char'

Create a cell array of character vectors containing the letters c, d, and e.

B = {'c','d','e'};
class(B)
ans = 
'cell'

Combine the elements of A and B.

C = union(A,B)
C = 5x1 cell
    {'a'}
    {'b'}
    {'c'}
    {'d'}
    {'e'}

The result, C, is a cell array of character vectors.

class(C)
ans = 
'cell'

Use the 'legacy' flag to preserve the behavior of union from R2012b and prior releases in your code.

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

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

     1     3     5     7

ia1 = 3×1

     3
     1
     2

ib1 = 1

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

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

     1     3     5     7

ia2 = 1×2

     1     2

ib2 = 1×2

     3     1

Input Arguments

collapse all

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

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 character vectors can combine with character arrays or string arrays.

  • Categorical arrays can combine with character arrays, cell arrays of character vectors, or string arrays.

  • Datetime arrays can combine with cell arrays of date character vectors or single date character vectors.

There are additional requirements for A and B based on data type:

  • 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 consist of the categories of A followed by the categories of B that are not in A. The categories are in the same order as in A and B, and the category order is used for sorting C.

  • If A and B are tables or timetables, they must have the same variable names (except for order). For tables, row names are ignored, so that two rows that have the same values, but different names, are considered equal. For timetables, row times are taken into account, so that two rows that have the same values, but different times, are not considered equal.

  • If A and B are datetime arrays, they must be consistent with each other in whether they specify a time zone.

A and B also 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. For example, A and B can be arrays of handles to graphics objects.

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

FlagDescription
'sorted'

The values (or rows) in C return in sorted order as returned by sort.

Example

C = union([5 5 3],[1 2 5],'sorted')
C =

     1     2     3     5

'stable'

The values (or rows) in C return in the same order as they appear in A, then B.

Example

C = union([5 5 3],[1 2 5],'stable')
C =

     5     3     1     2

Data Types: char | string

Output Arguments

collapse all

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

The following describes the shape of C when the inputs are vectors 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, in which case C is a row vector. For example, union([],[1 2]) returns a column 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 character vectors with char, then C is a cell array of character vectors.

  • If you combine a categorical array with a character vector, cell array of character vectors, or string, then C is a categorical array.

  • If you combine a datetime array with a cell array of date character vectors or single date character vector, then C is a datetime array.

  • If you combine a string array with a character vector or cell array of character vectors, then C is a string 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).

Tips

  • 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(:,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. Alternatively, you can use vartype to create a subscript that selects variables of a specified type.

Extended Capabilities

Thread-Based Environment
Run code in the background using MATLAB® backgroundPool or accelerate code with Parallel Computing Toolbox™ ThreadPool.

Version History

Introduced before R2006a