Scilab Website | Contribute with GitLab | Mailing list archives | ATOMS toolboxes
Scilab Online Help
6.0.1 - English

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

Please note that the recommended version of Scilab is 2024.0.0. This page might be outdated.
See the recommended documentation of this function

Scilab Help >> Elementary Functions > Set operations > unique

unique

extracts (and sorts) distinct components of a vector or a matrix

Syntax

[N ,k] = unique(M)
[N ,k] = unique(M, orient)

Arguments

M

vector or matrix of numbers or strings.

orient

flag with possible values : 1 or "r", 2 or "c"

N

  • If orient is not used: Vector of extracted M components sorted in ascending order.
  • 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)).

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.

Extracted components, rows or columns can be resorted in their initial order by sorting k:
[N, k] = unique(M);      k = gsort(k,"g",i); N = N(k)
[N, k] = unique(M, "c"); k = gsort(k,"g",i); N = N(:, k)
[N, k] = unique(M, "r"); k = gsort(k,"g",i); N = N(k, :)

Examples

M = round(2*rand(20,1));

unique(M)
[N,k] = unique(M)

unique(string(M))
[N,k] = unique(string(M))

A = [0,0,1,1;
     0,1,1,1;
     2,0,1,1;
     0,2,2,2;
     2,0,1,1;
     0,0,1,1];
T = 'x'+string(A);

//unique rows

[m,k] = unique(A,'r')
unique(T,'r')

//unique columns
[m,k] = unique(T,'c')
unique(A,'c')

See also

  • members — count (and locate) in an array each element or row or column of another array
  • gsort — sorting by quick sort algorithm
  • vectorfind — finds in a matrix rows or columns matching a vector
  • grep — find matches of a string in a vector of strings
  • union — extract union components of a vector
  • intersect — returns the vector of common values of two vectors
Report an issue
<< union Set operations Signal processing >>

Copyright (c) 2022-2023 (Dassault Systèmes)
Copyright (c) 2017-2022 (ESI Group)
Copyright (c) 2011-2017 (Scilab Enterprises)
Copyright (c) 1989-2012 (INRIA)
Copyright (c) 1989-2007 (ENPC)
with contributors
Last updated:
Mon Feb 12 19:26:47 CET 2018