union - Union of two sets of data - MATLAB (original) (raw)
Union of two sets of data
Syntax
Description
[C](#btcme1c-1-C) = union([A,B](#btcme1c-1%5Fsep%5Fshared-AB))
returns the combined data from A
and B
with no repetitions. C
is in sorted order.
- If
A
andB
are tables or timetables, thenunion
returns the combined set of rows from both tables. For timetables,union
takes row times into account to determine equality, and sorts the output timetableC
by row times.
[C](#btcme1c-1-C) = union([A,B](#btcme1c-1%5Fsep%5Fshared-AB),[setOrder](#btcme1c-1-setOrder))
returns C
in a specific order. setOrder
can be 'sorted'
or 'stable'
.
[C](#btcme1c-1-C) = union([A,B](#btcme1c-1%5Fsep%5Fshared-AB),___,'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
andB
, with no repetitions. You must specifyA
and B
and optionally can specifysetOrder
.
The 'rows'
option does not support cell arrays, unless one of the inputs is either a categorical array or a datetime array.
[[C](#btcme1c-1-C),[ia](#btcme1c-1-ia),[ib](#btcme1c-1-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 ofA(ia)
andB(ib)
. - If the
'rows'
option is specified, then the rows ofC
are a sorted combination of the rows ofA(ia,:)
andB(ib,:)
. - If
A
andB
are tables or timetables, thenC
is a sorted combination of the rows ofA(ia,:)
andB(ib,:)
.
[[C](#btcme1c-1-C),[ia](#btcme1c-1-ia),[ib](#btcme1c-1-ib)] = union([A,B](#btcme1c-1%5Fsep%5Fshared-AB),'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
Define two vectors with a value in common.
A = [5 7 1]; B = [3 1 1];
Find the union of vectors A
and B
.
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=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
.
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=4×3 table Gender Age Height ______ ___ ______
Betty F 31 64
Ted M 27 74
Joe M 47 68
Fred M 52 68
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
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')
Alternatively, you can specify 'sorted'
order.
A = [5 7 1]; B = [3 1 1]; [C,ia,ib] = union(A,B,'sorted')
Define two vectors containing NaN
.
A = [5 NaN 1]; B = [4 NaN NaN];
Find the union of vectors A
and 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 = 1×6 cell {'cat'} {'dog'} {'dog '} {'fish'} {'fish '} {'horse'}
union
treats trailing white space in cell arrays of character vectors as distinct characters.
Create a column vector character array.
A = 3×1 char array 'A' 'B' 'C'
Create a row vector containing elements of numeric type double
.
The union
of A
and B
returns a column vector character array.
C = 6×1 char array 'A' 'B' 'C' 'D' 'E' 'F'
Create a character vector containing the letters a
, b
, and c
.
A = ['a';'b';'c']; class(A)
Create a cell array of character vectors containing the letters c
, d
, and e
.
B = {'c','d','e'}; class(B)
Combine the elements of A
and B
.
C = 5×1 cell {'a'} {'b'} {'c'} {'d'} {'e'}
The result, C
, is a cell array of character vectors.
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)
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')
Input Arguments
Order flag, specified as 'sorted'
or'stable'
, indicates the order of the values (or rows) in C
.
Flag | Description |
---|---|
'sorted' | The values (or rows) in C return in sorted order as returned by sort.ExampleC = 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 inA, thenB.ExampleC = union([5 5 3],[1 2 5],'stable')C = 5 3 1 2 |
Data Types: char
| string
Output Arguments
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, thenC
is a column vector unless bothA
andB
are row vectors, in which caseC
is a row vector. For example,union([],[1 2])
returns a column vector. - If the
'rows'
flag is specified, thenC
is a matrix containing the combined rows ofA
andB
.
The class of the inputs A
and B
determines the class of C
:
- If the class of
A
andB
are the same, thenC
is the same class. - If you combine a
char
or nondouble numeric class withdouble
, thenC
is the same class as the nondouble input. - If you combine a
logical
class withdouble
, thenC
isdouble
. - If you combine a cell array of character vectors with
char
, thenC
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 inA
, then ia
contains the index to the first occurrence of the value (or row). If a value appears in bothA
and B
, thenia
contains the index to the first occurrence inA
.
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 inB
, then ib
contains the index to the first occurrence of the value. If a value (or row) appears in bothA
and B
, thenib
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`_))
, wherevars
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
Theunion
function supports tall arrays with the following usage notes and limitations:
- The
'stable'
sorting order is not supported. - The
'legacy'
flag is not supported. - Inputs of type
char
are not supported. - Ordinal categorical arrays are not supported.
For more information, see Tall Arrays.
Usage notes and limitations:
- Code generation does not support cell arrays for the first or second arguments.
- Code generation does not support
union
between a variable-size rows and columns.A
andB
must be variable-size vector inputs with the same orientation. - When you do not specify the
'rows'
option:- Inputs
A
andB
must be vectors with the same orientation. If you specify the'legacy'
option, then inputsA
andB
must be row vectors. - The first dimension of a variable-size row vector must have fixed length 1. The second dimension of a variable-size column vector must have fixed length 1.
- The input
[]
is not supported. Use a 1-by-0 or 0-by-1 input, for example ,zeros(1,0)
, to represent the empty set. - If you specify the
'legacy'
option, then empty outputs are row vectors, 1-by-0. They are never 0-by-0.
- Inputs
- When you specify both the
'legacy'
option and the'rows'
option, the outputsia
andib
are column vectors. If these outputs are empty, they are 0-by-1. They are never 0-by-0, even if the outputC
is 0-by-0. - When the
setOrder
is not'stable'
or when you specify the'legacy'
option, the inputs must already be sorted in ascending order. The first output,C
, is sorted in ascending order. - Complex inputs must be
single
ordouble
. - When one input is complex and the other input is real, do one of the following:
- Set
setOrder
to'stable'
. - Sort the real input in complex ascending order (by absolute value). Suppose the real input is
x
. Usesort(complex(x))
orsortrows(complex(x))
.
- Set
- See Code Generation for Complex Data with Zero-Valued Imaginary Parts (MATLAB Coder).
The union
function supports GPU array input with these usage notes and limitations:
- The
'legacy'
flag is not supported. - 64-bit integers are not supported.
For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Usage notes and limitations:
- The
'legacy'
flag is not supported. - Table, timetable, categorical, datetime and duration inputs are not supported.
- Inputs of type
char
andstring
are not supported whenA
orB
is a cell array of character vectors. Convert cell arrays of character vectors input arguments to string arrays instead.
For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
Version History
Introduced before R2006a