matlab unique
时间: 2023-10-16 13:13:04 浏览: 33
The function "unique" in MATLAB returns an array containing only the unique elements of the input array. It can be used with one or more input arguments, and can also return the indices of the unique elements in the original array.
Syntax:
```
[B,I] = unique(A)
```
where A is the input array and B is the array of unique elements, and I is an index vector such that B = A(I).
Example:
```
A = [1 2 3 2 4 1];
B = unique(A);
```
The output of this code will be:
```
B = [1 2 3 4]
```
Note that the order of the elements in B is not necessarily the same as in A. If you want to preserve the order, you can use the 'stable' option:
```
[B,I] = unique(A,'stable');
```
The output of this code will be:
```
B = [1 2 3 4]
I = [1 2 3 5 6 4]
```
Here, I is a vector of indices such that B = A(I) and the order of the elements in B is the same as in A.