next up previous contents
Next: SORT Sort Up: Array Generation and Manipulations Previous: BIN2INT Convert Binary Arrays   Contents

Subsections

UNIQUE Unique

Usage

Returns a vector containing the unique elements of an array. The first form is simply

   y = unique(x)

where x is either a numerical array or a cell-array of strings. The result is sorted in increasing order. You can also retrieve two sets of index vectors

   [y, m, n] = unique(x)

such that y = x(m) and x = y(n). If the argument x is a matrix, you can also indicate that FreeMat should look for unique rows in the matrix via

   y = unique(x,'rows')

and

   [y, m, n] = unique(x,'rows')

Example

Here is an example in row mode

--> A = randi(1,3*ones(15,3))
A = 
  <int32>  - size: [15 3]
 
Columns 1 to 3
             2              3              2  
             2              1              1  
             2              2              3  
             2              1              3  
             2              2              3  
             2              1              2  
             1              2              2  
             1              1              1  
             3              1              3  
             2              2              2  
             1              3              3  
             1              2              3  
             3              1              1  
             3              3              1  
             2              3              3  
--> unique(A,'rows')
ans = 
  <int32>  - size: [14 3]
 
Columns 1 to 3
             1              1              1  
             1              2              2  
             1              2              3  
             1              3              3  
             2              1              1  
             2              1              2  
             2              1              3  
             2              2              2  
             2              2              3  
             2              3              2  
             2              3              3  
             3              1              1  
             3              1              3  
             3              3              1  
--> [b,m,n] = unique(A,'rows');
--> b
ans = 
  <int32>  - size: [14 3]
 
Columns 1 to 3
             1              1              1  
             1              2              2  
             1              2              3  
             1              3              3  
             2              1              1  
             2              1              2  
             2              1              3  
             2              2              2  
             2              2              3  
             2              3              2  
             2              3              3  
             3              1              1  
             3              1              3  
             3              3              1  
--> A(m,:)
ans = 
  <int32>  - size: [14 3]
 
Columns 1 to 3
             1              1              1  
             1              2              2  
             1              2              3  
             1              3              3  
             2              1              1  
             2              1              2  
             2              1              3  
             2              2              2  
             2              2              3  
             2              3              2  
             2              3              3  
             3              1              1  
             3              1              3  
             3              3              1  
--> b(n,:)
ans = 
  <int32>  - size: [15 3]
 
Columns 1 to 3
             2              3              2  
             2              1              1  
             2              2              3  
             2              1              3  
             2              2              3  
             2              1              2  
             1              2              2  
             1              1              1  
             3              1              3  
             2              2              2  
             1              3              3  
             1              2              3  
             3              1              1  
             3              3              1  
             2              3              3

Here is an example in vector mode

--> A = randi(1,5*ones(10,1))
A = 
  <int32>  - size: [10 1]
 
Columns 1 to 1
             4  
             3  
             5  
             3  
             2  
             5  
             5  
             2  
             3  
             4  
--> unique(A)
ans = 
  <int32>  - size: [4 1]
 
Columns 1 to 1
             2  
             3  
             4  
             5  
--> [b,m,n] = unique(A,'rows');
--> b
ans = 
  <int32>  - size: [4 1]
 
Columns 1 to 1
             2  
             3  
             4  
             5  
--> A(m)
ans = 
  <int32>  - size: [4 1]
 
Columns 1 to 1
             2  
             3  
             4  
             5  
--> b(n)
ans = 
  <int32>  - size: [10 1]
 
Columns 1 to 1
             4  
             3  
             5  
             3  
             2  
             5  
             5  
             2  
             3  
             4

For cell arrays of strings.

--> A = {'hi','bye','good','tell','hi','bye'}
A = 
  <cell array> - size: [1 6]
 
Columns 1 to 6
 hi  bye  good  tell  hi  bye  
--> unique(A)
ans = 
  <cell array> - size: [4 1]
 
Columns 1 to 1
 bye  
 good  
 hi  
 tell



Samit K. Basu 2005-03-16