union - Find set union of two vectors

Syntax

c = union(A, B)
c = union(A, B, 'rows')
[c, ia, ib] = union(...)

Description

c = union(A, B) returns the combined values from A and B but with no repetitions. In set theoretic terms, c = AB. Inputs A and B can be numeric or character vectors or cell arrays of strings. The resulting vector is sorted in ascending order.

c = union(A, B, 'rows') when A and B are matrices with the same number of columns returns the combined rows from A and B with no repetitions. MATLAB ignores the rows flag for all cell arrays.

[c, ia, ib] = union(...) also returns index vectors ia and ib such that c = a(ia)b(ib), or for row combinations, c = a(ia,:)b(ib,:). If a value appears in both a and b, union indexes its occurrence in b. If a value appears more than once in b or in a (but not in b), union indexes the last occurrence of the value.

Remarks

Because NaN is considered to be not equal to itself, every occurrence of NaN in A or B is also included in the result c.

Examples

a = [-1 0 2 4 6];     
b = [-1 0 1 3];
[c, ia, ib] = union(a, b);
c =

    -1     0     1     2     3     4     6

ia =

     3     4     5

ib =

     1     2     3     4

See Also

intersect, setdiff, setxor, unique, ismember, issorted

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

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