Please note that the recommended version of Scilab is 2024.1.0. This page might be outdated.
See the recommended documentation of this function
unique
extracts (and sorts) distinct elements, rows or columns of a matrix
Syntax
[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")
Arguments
 M
 vector, matrix, or hypermatrix of booleans, numbers, or text. Numeric or boolean sparses are accepted.
 orient
 flag with possible values : 1 or "r", 2 or "c". It can't be used if
M
is an hypermatrix.  U
 If
orient
is not used: Vector of extractedM
components sorted in ascending order. IfM
is a row vector,U
is also a row vector. In all otherM
cases,U
is a column vector.  If
orient
is used: Matrix of extractedM
rows or columns, sorted in lexicographic ascending order.
M
is sparse, thenU
is always sparse. If
 km
 Vector of indices of first encountered occurrences, such that
U = M(km)
orU = M(km,:)
orU = M(:,km)
.km
is a row ifM
is a row or iforient="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), andU(ku) = M
 "r":
ku
is of size [size(M,1), 1], andU(ku,:) = M
 "c":
ku
is of size [1, size(M,2)], andU(:,ku) = M
ku
is dense, even whenM
is sparse and the overall "*" mode is used.  "*":
 nb
 Vector of integers > 0, with the same
km
shape: Numbers of occurrences inM
of respective unduplicated entities (components, rows, columns) returned inU
.
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 caseinsensitive.
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 caseinsensitive.
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 [π,π]. 
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, 1i, i, i, i, 1+i] [u, k] = unique(c) [uc, kc] = unique(c, "c")
> c = [1+i, 1i, 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, ku, 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, 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. > [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.
See also
 members — count (and locate) in an array each element or row or column of another array
 gsort — sorts boolean, numerical and string arrays
 vectorfind — locates occurences of a (wildcarded) vector in a matrix or hypermatrix
 grep — find matches of a string in a vector of strings
 union — Set of all elements, rows, or columns of two arrays, without duplicates
 intersect — elements or rows or columns met in both input arrays, without duplicates
History
Version  Description 
6.0.2  unique() can now be used to unduplicate complex numbers. 
6.1.0 

6.1.1  ku 3rd output implemented. Sparse 2D matrices are now accepted. 
Report an issue  
<< union  Set operations  Trigonometry >> 