union
Set of all elements, rows, or columns of two arrays, without duplicates
Syntax
[v, ka, kb] = union(a, b) [v, ka, kb] = union(a, b, orient)
Arguments
- a, b
- can be:
- vectors or matrices of booleans, doubles, strings, duration or
datetime. Sparse boolean or numerical matrices
are accepted. The types of
a
and ofb
can be distinct but compatible for concatenation. - table and timeseries are also accepted:
a
andb
must have the same variable names.
- vectors or matrices of booleans, doubles, strings, duration or
datetime. Sparse boolean or numerical matrices
are accepted. The types of
- orient
- orientation of the processing:
- 1 or "r": the union is performed over the rows.
- 2 or "c": the union is performed over the columns.
- v
- depending on type of
a
andb
:- row vector, or matrix.
v
's data type is the type of[a(:) ; b(:)]
's result. - table or timeseries
- row vector, or matrix.
- ka
- row vector of integers: Indices in
a
ofv
elements/rows/columns coming froma
. - kb
- row vector of integers: Indices in
b
ofv
remaining elements/rows/columns coming fromb
.
Description
union(a,b)
, with a
and b
vectors or matrices,
returns a sorted row vector or matrix which
retains the unique entries of [a(:) ; b(:)]
.
If a
and b
are table or timeseries, union(a, b)
returns the sorted unique rows contained
in a
and b
.
union(a,b,"r")
or
union(a,b,1)
returns the matrix formed by the union of
the unique rows of a
and b
sorted in
lexicographic ascending order. In this case matrices a
and b
must have the same number of columns.
union(a,b,"c")
or
union(a,b,2)
returns the matrix formed by the union of
the unique columns of a
and b
sorted
in lexicographic ascending order. In this case matrices
a
and b
must have the same number of
rows.
The orient
option is not managed for table and timeseries.
[v,ka,kb]=union(a,b)
also returns index vectors
ka
and kb
such that
v
is a sorted combination of the entries
a(ka)
and b(kb)
.
Examples
A = [6 7 6 ; 5 8 3 ]; B = [1 7 1 0 6 ]; union(A, B) [u, ka, kb] = union(A, B)
--> union(A, B) ans = 0. 1. 3. 5. 6. 7. 8. --> [u, ka, kb] = union(A, B) u = 0. 1. 3. 5. 6. 7. 8. ka = 6. 2. 1. 3. 4. kb = 4. 1.
A = ["a" "b" "a" "c" "c" "b" "b" "c" "a" "b" "c" "c" ]; B = ["b" "a" "c" "c" "b" "a" "a" "c" "b" "b" "b" "b" ]; [U, ka, kb] = union(A, B, "c")
--> [U, ka, kb] = union(A, B, "c") U = "a" "a" "a" "b" "b" "b" "c" "c" "a" "b" "c" "a" "b" "c" "b" "c" ka = 3. 1. 2. 4. 5. kb = 2. 1. 5.
[F, T] = (%f, %t); A = sparse([T T F T F T ; F F F F T T ; T F F F F T ]); full(A) B = sparse([F F T T F F ; T T T T T T ; T F T T T F ]); full(B) [U, ka, kb] = union(A, B, "c"); issparse(U) full(U), ka, kb
--> A = sparse([T T F T F T ; F F F F T T ; T F F F F T ]); full(A) ans = T T F T F T F F F F T T T F F F F T --> B = sparse([F F T T F F ; T T T T T T ; T F T T T F ]); full(B) ans = F F T T F F T T T T T T T F T T T F --> [U, ka, kb] = union(A, B, "c"); --> issparse(U) ans = T --> full(U), ka, kb ans = F F F T T T F T T F F T F F T F T T ka = 3. 5. 2. 1. 6. kb = 1.
union()
for duration and datetime:
A = hours([1 7 7 4 3 6 6]); B = hours([7 5 3 0]); [u, ka, kb] = union(A, B) A = datetime("today") + A; B = datetime("today") + B; [u, ka, kb] = union(A, B)
union()
for table:
A = table([1 7 7 4 3 6 6]', [1:7]'); B = table([7 5 3 0]', [2;2;5;5]); [u, ka, kb] = union(A, B) A = table([1;3;4;2], ["d";"c";"f";"h"], [%f;%t;%t; %f], "VariableNames", ["double", "string", "boolean"]); B = table([2;3;4;1], [%t;%t;%t;%t], ["d";"c";"f";"h"], "VariableNames", ["double", "boolean", "string"]); [u, ka, kb] = union(A, B)
union()
for timeseries:
A = timeseries(hours([1:10])', floor(rand(10,1)*10)); B = timeseries(hours(1:2:15)', floor(rand(8,1)*15)); [u, ka, kb] = union(A, B)
See also
History
バージョン | 記述 |
6.1.0 | Extension to boolean matrices. |
6.1.1 | Extension to sparse boolean, sparse real, and sparse complex matrices. |
2026.0.0 | Support for duration, datetime, table and timeseries types added. |
Report an issue | ||
<< setdiff | setoperations | unique >> |