| Contents | Index |
In a future release, the behavior of unique 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 unique. For a detailed explanation of all the forthcoming changes, see Set Functions Changing Behavior in a Future Release.
C = unique(A) for the array A, returns the same values as in A but with no repetitions. The values of C are in sorted order.
C = unique(A,'rows') for the matrix A, returns the unique rows of A. The rows of the matrix C are in sorted order.
[C,ia,ic] = unique(A,'rows') also returns index vectors ia and ic such that C = A(ia,:) and A = C(ic,:).
[C,ia,ic] = unique(A,occurrence) and [C,ia,ic] = unique(A,'rows',occurrence) specify which index is returned in ia when there are repeated values (or rows) in A. The default value is occurrence ='last', and unique returns the index of the last occurrence of each unique value (or row) in A. If occurrence='first', unique returns the index of the first occurrence of each unique value (or row).
[C,ia,ic] = unique(___,'R2012a') adopts the future behavior of the unique function. [C,ia,ic] = unique(___,'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, 'rows', or occurrence.
A = [9 2 9 5]; C = unique(A)
C =
2 5 9A = [9 2 9 5]; [C, ia, ic] = unique(A)
C =
2 5 9
ia =
2 4 3
ic =
3 1 3 2A = [9 2 9 5; 9 2 9 0; 9 2 9 5];
[C, ia, ic] = unique(A,'rows')C =
9 2 9 0
9 2 9 5
ia =
2
3
ic =
2
1
2Notice that the second row in A is identified as unique even though 9, 2, and 9 are repeated in the other rows.
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.
A = [9 2 9 5];
[C, ia, ic] = unique(A,'stable')C =
9 2 5
ia =
1
2
4
ic =
1
2
1
3
Alternatively, you can specify 'sorted' order.
[C, ia, ic] = unique(A,'sorted')C =
2 5 9
ia =
2
4
1
ic =
3
1
3
2unique 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 ic conforms to the current behavior. Compare the sorted output here to the output from the example, Find Unique Values and Their Indices.
Specify whether ia should contain the first or last index to repeated values.
Specify the first occurrence of 9 in A.
A = [9 2 9 5];
[C1, ia1, ic1] = unique(A,'first')C1 =
2 5 9
ia1 =
2 4 1
ic1 =
3 1 3 2
Alternatively, you can specify the last occurrence of 9 in A.
A = [9 2 9 5];
[C2, ia2, ic2] = unique(A,'last')C2 =
2 5 9
ia2 =
2 4 3
ic2 =
3 1 3 2Use 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 unique elements of A with the current default behavior.
A = [9 2 9 5]; [C1, ia1, ic1] = unique(A)
C1 =
2 5 9
ia1 =
2 4 3
ic1 =
3 1 3 2Find the unique elements of A, and opt into the forthcoming behavior. In the future, this behavior will be the default.
[C2, ia2, ic2] = unique(A, 'R2012a')C2 =
2 5 9
ia2 =
2
4
1
ic2 =
3
1
3
2
ia1 and ia2 have different shapes and content, while ic1 and ic2 differ only in shape.
Find the unique elements of A, and preserve the current behavior.
[C3, ia3, ic3] = unique(A, 'legacy')C3 =
2 5 9
ia3 =
2 4 3
ic3 =
3 1 3 2C3, ia3, and ic3 match C1, ia1, and ic1 respectively.
A = [5 5 NaN NaN]; C = unique(A)
C =
5 NaN NaNunique treats NaN values as distinct.
C | Unique Values of A Unique values of A 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 unique rows of A. |
ia | Index to A Index to A is a vector that contains indices such that C = A(ia). |
ic | Index to C Index to C is a vector that contains indices such that A = C(ic). |
intersect | ismember | issorted | setdiff | setxor | sort | union
| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |