Scilab Home page | Wiki | Bug tracker | Forge | Mailing list archives | ATOMS | File exchange
Change language to: English - Français - 日本語 - Русский

# unique

remover todos os componentes duplicados de um vetor ou uma matriz

### Seqüência de Chamamento

```[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")```

### Parâmetros

M

vetor ou matriz de números ou strings

orient

flag com valores possíveis : 1 ou "r", 2 ou "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.

k

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

`k` 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 `k` shape: Numbers of occurences in `M` of respective unduplicated entities (components, rows, columns) returned in `N`.

### Descrição

`unique(M)` retorna um vetor que retém as entradas únicas de `M` em ordem ascendente.

`unique(M,"r")` ou `unique(M,1)`retorna as linhas únicas de `M` em ordem lexicográfica ascendente.

`unique(M,"c")` ou `unique(M,2)`retorna as linhas únicas de `M` em ordem lexicográfica ascendente.

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

### Exemplos

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

### Ver Também

• 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 — acha correspondências de um string em um vetor de strings
• union — extrai componentes da união de um vetor
• intersect — returns the unduplicated elements or rows or columns met in both input arrays

### Histórico

 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.