| Contents | Index |
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.
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.
[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'.
A = [5 7 1]; B = [3 1 1]; C = union(A,B)
C =
1 3 5 7A = [5 7 1]; B = [3 1 1]; [C,ia,ib] = union(A,B)
C =
1 3 5 7
ia =
1 2
ib =
3 1
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
3Use 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 =
1Alternatively, 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 =
1union 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.
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 1Find 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 =
1Notice 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 1C3, ia3, and ib3 match C1, ia1, and ib1, respectively.
A = [5 NaN 1]; B = [4 NaN NaN]; C = union(A,B)
C =
1 4 5 NaN NaN NaNunion treats NaN values as distinct.
C | Combined Values of A and B 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
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
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). |
intersect | ismember | issorted | setdiff | setxor | sort | unique
| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |