# unique

ベクトルまたは行列のユニークなな要素を展開

### 呼び出し手順

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

### 引数

M

vector, matrix, or hypermatrix of booleans, numbers, or strings. Numeric or boolean sparses are accepted.

orient

フラグで以下の値のどれかとなる : 1 または "r", 2 または "c". It can't be used if `M` is an hypermatrix.

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

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

If `M` is sparse, then `U` is always sparse.

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

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

ku
Array of indices in U, such that, according to the `orient` option
• "*": `ku` is of size size(M), and `U(ku) = M`
• "r": `ku` is of size [size(M,1), 1], and `U(ku,:) = M`
• "c": `ku` is of size [1, size(M,2)], and `U(:,ku) = M`

 `ku` is dense, even when `M` is sparse and the overall "*" mode is used.
nb

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

### 説明

`unique(M)``M`のユニークなエントリを昇順に したものを保持するベクトルを返します.

`unique(M,"r")` または `unique(M,1)`は, `M`のユニークな行を 辞書式の昇順にして返します.

`unique(M,"c")` または `unique(M,2)``M`のユニークな列を 辞書式の昇順にして返します.

`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.

For booleans, `unique(…)` is useful mainly with the "r" or "c" options.

 Complex numbers are sorted first by increasing magnitudes, then by increasing phases on [-π,π].

### 例

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    0. - i    0. + i    0. - i    1. + i

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

--> [uc, kc] = unique(c, "c")
uc  =
0. - i    0. + i    1. - i    1. + i
kc  =
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, nb] = unique(t(1,:))
[u, kt] = unique(t(1,:), "keepOrder")      // In original order of row#1 elements
[uc, ktc, kuc, nb] = unique(t, "c")
[uc, ktc, kuc, nb] = unique(t, "c", "keepOrder")  // In original order of columns
[and(t(:,ktc)==uc), and(uc(:,kuc)==t) ]```
```--> 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  =
!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, kc, 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  !

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

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

--> [and(t(:,ktc)==uc), and(uc(:,kuc)==t) ]
ans  =
T  T
```

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 — 配列の各要素または他の配列の行または列を数える(及び位置を調べる)
• gsort — sorts boolean, numerical and string arrays
• vectorfind — locates occurences of a (wildcarded) vector in a matrix or hypermatrix
• grep — 文字列のベクトルの中で指定した文字列に一致するかどうかを調べる
• union — ベクトルの和集合要素を展開
• intersect — elements or rows or columns met in both input arrays, without duplicates

### 履歴

 バージョン 記述 6.0.2 unique() can now be used to unduplicate complex numbers. 6.1.0 Extension to booleans. "keepOrder" and "uniqueNan" options introduced. Fourth output argument `nb` introduced. 6.1.1 ku 3rd output implemented. Sparse 2D matrices are now accepted.
 Report an issue << union setoperations Trigonometry >>