Change language to:
Français - 日本語 - Português - Русский

See the recommended documentation of this function

unique

extracts (and sorts) distinct elements, rows or columns of a matrix

Syntax

```[N, km, kn, nb] = unique(M)
[N, km, kn, nb] = unique(M, orient)
[N, km, kn, nb] = unique(.., "keepOrder")
[N, km, kn, nb] = unique(.., "uniqueNan")```

Arguments

M
vector, matrix, or hypermatrix of numbers or of strings.

orient
flag with possible values : 1 or "r", 2 or "c". It can't be used if `M` is an hypermatrix.

N
• If `orient` is not used: Vector of extracted `M` components sorted in ascending order. If `M` is a row vector, `N` is also a row vector. In all other `M` cases, `N` is a matrix or a column vector.

• If `orient` is used: Matrix of extracted `M` rows or columns, sorted in lexicographic ascending order.

km
Vector of indices of first encountered occurrences, such that `N(i) = M(km(i))` or `N(i,:) = M(km(i),:)` or `N(:,i) = M(:,km(i))`.

`km` is a row if `M` is a row or if `orient="c"` is used. Otherwise it's a column.

kn
Not yet implemented. Vector of indices of first encountered occurrences, such that `M(i) = N(kn(i))` or `M(i,:) = N(kn(i),:)` or `M(:,i) = N(:,kn(i))`.

nb
Vector of integers > 0, with the same `km` shape: Numbers of occurrences in `M` of respective unduplicated entities (components, rows, columns) returned in `N`.

Description

`unique(M)` returns a vector which retains the unique entries of `M` in ascending order.

`unique(M,"r")` or `unique(M,1)` removes all duplicates of `M` rows and returns unique rows in lexicographic ascending order.

`unique(M,"c")` or `unique(M,2)` removes all duplicates of `M` columns and returns unique columns in lexicographic ascending order.

`unique(M,.. "keepOrder")` returns `M` unduplicated entries in their original order in `M`. `"keepOrder"` is case-insensitive.

`unique(M,.. "uniqueNan")` considers all Nan values as the same one, and unduplicates them. By default, any Nan is different from any other Nan, including itself: `%nan<>%nan` is true, unless `"uniqueNan"` is used. Specifying `"uniqueNan"` is case-insensitive.

Examples

With some numbers:

```M = int8([2  0  2  2  1  1  1  2  1  1  0  1  1  0  1  1
0  1  2  0  1  2  2  0  1  1  2  0  1  0  0  0
])
[u, km] = unique(M)
[uc, kmc] = unique(M, "c")```
```--> M
M =
2  0  2  2  1  1  1  2  1  1  0  1  1  0  1  1
0  1  2  0  1  2  2  0  1  1  2  0  1  0  0  0

--> [u, km] = unique(M)
u  =
0
1
2

km  =
2.
4.
1.

--> [uc, kmc] = unique(M, "c")
uc  =
0  0  0  1  1  1  2  2
0  1  2  0  1  2  0  2

kmc  =
14.   2.   11.   12.   5.   6.   1.   3.
```

With complex numbers:

```i = %i;
c = [1+i, 1-i, -i, i, -i, 1+i]
[u, k] = unique(c)
[uc, kc] = unique(c, "c")```
```--> c = [1+i, 1-i, -i, i, -i, 1+i]
c  =
1. + i     1. - i  -i    i   -i    1. + i

--> [u, k] = unique(c)
u  =
-i    i    1. - i   1. + i

k  =
3.   4.   2.   1.
```

With some texts:

```t = ["BA" "BB" "AB" "BA" "AB" "BA" "AB" "AB" "BA" "AA" "AB" "BA" "BA" "BA" "AA"
"AA" "AB" "AA" "AA" "BB" "BB" "BB" "BA" "AB" "AB" "BB" "BB" "AB" "AB" "AA"
]
u = unique(t)'
[u, kt, ku, nb] = unique(t(1,:))
[u, kt] = unique(t(1,:), "keepOrder")  // Keeping the original order of row#1 elements
[uc, ktc, kuc, nb] = unique(t, "c")
[uc, ktc, kuc, nb] = unique(t, "c", "keepOrder")  // Keeping the original order of columns```
```--> t = ["BA" "BB" "AB" "BA" "AB" "BA" "AB" "AB" "BA" "AA" "AB" "BA" "BA" "BA" "AA"
>      "AA" "AB" "AA" "AA" "BB" "BB" "BB" "BA" "AB" "AB" "BB" "BB" "AB" "AB" "AA"
>     ]
t  =
!BA  BB  AB  BA  AB  BA  AB  AB  BA  AA  AB  BA  BA  BA  AA  !
!AA  AB  AA  AA  BB  BB  BB  BA  AB  AB  BB  BB  AB  AB  AA  !

--> u = unique(t)'
u  =
!AA  AB  BA  BB  !

--> [u, kt, ku, nb] = unique(t(1,:)); u, kt, nb
u  =
!AA  AB  BA  BB  !

kt  =
10.   3.   1.   2.

nb  =
2.   5.   7.   1.

--> [u, kt] = unique(t(1,:), "keepOrder")  // Keeping the original order
u  =
!BA  BB  AB  AA  !

kt  =
1.   2.   3.   10.

--> [uc, ktc, kuc, nb] = unique(t, "c")
uc  =
!AA  AA  AB  AB  AB  BA  BA  BA  BB  !             Sorted columns
!AA  AB  AA  BA  BB  AA  AB  BB  AB  !

ktc  =
15.   10.   3.   8.   5.   1.   9.   6.   2.

nb  =
1.   1.   1.   1.   3.   2.   3.   2.   1.

--> [uc, ktc, kuc, nb] = unique(t, "c", "keepOrder")  // Keeping the original order
uc  =
!BA  BB  AB  AB  BA  AB  BA  AA  AA  !
!AA  AB  AA  BB  BB  BA  AB  AB  AA  !

ktc  =
1.   2.   3.   5.   6.   8.   9.   10.   15.

nb  =
2.   1.   1.   3.   2.   1.   3.   1.   1.
```

With Nan (and Inf) values. "uniqueNan" option:

```M = [2  2  %nan  1     2  0     1  %nan  0    %nan
1  0  1     %nan  0  %inf  0  1     %inf 1
];
[v, km, kv, n] = unique(M); v',n'
[v, km, kv, n] = unique(M, "uniqueNan"); v',n'
unique(M, "c")
[v, kmc, kvc, n] = unique(M, "c", "uniqueNan")```
```--> M
M  =
2.   2.   Nan   1.    2.   0.    1.   Nan   0.    Nan
1.   0.   1.    Nan   0.   Inf   0.   1.    Inf   1.

--> [v, km, kv, n] = unique(M); v',n'
ans  =
0.   1.   2.   Inf   Nan   Nan   Nan   Nan

ans  =
5.   6.   3.   2.   1.   1.   1.   1.

--> [v, km, kv, n] = unique(M, "uniqueNan"); v',n'
ans  =
0.   1.   2.   Inf   Nan

ans  =
5.   6.   3.   2.   4.

--> unique(M, "c")
ans  =
0.    1.   1.    2.   2.   Nan   Nan   Nan
Inf   0.   Nan   0.   1.   1.    1.    1.

--> [v, kmc, kvc, n] = unique(M, "c", "uniqueNan")
v  =
0.    1.   1.    2.   2.   Nan
Inf   0.   Nan   0.   1.   1.

kmc  =
6.   7.   4.   2.   1.   3.

n  =
2.   1.   1.   2.   1.   3.
```

• members — count (and locate) in an array each element or row or column of another array
• gsort — sorting by quick sort algorithm
• vectorfind — locates occurences of a (wildcarded) vector in a matrix or hypermatrix
• grep — find matches of a string in a vector of strings
• union — extract union components of a vector
• intersect — returns the unduplicated elements or rows or columns met in both input arrays

History

 Version Description 6.0.2 unique() can now be used to unduplicate complex numbers. 6.1.0 "keepOrder" and "uniqueNan" options introduced. Fourth output argument `nb` introduced.
 Report an issue << union Set operations Trigonometry >>