FreeMat: UNIQUE Unique

FreeMat

- Main Page
- Related Pages

UNIQUE Unique

Section: Array Generation and Manipulations

# 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 =
     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 =
     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 =
     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 =
     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 =
     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 =
     5
     5
     5
     3
     5
     3
     4
     1
     3
     2

    --> unique(A)

    ans =
     1
     2
     3
     4
     5

    --> [b,m,n] = unique(A,'rows');
    --> b

    ans =
     1
     2
     3
     4
     5

    --> A(m)

    ans =
     1
     2
     3
     4
     5

    --> b(n)

    ans =
     5
     5
     5
     3
     5
     3
     4
     1
     3
     2

For cell arrays of strings.

    --> A = {'hi','bye','good','tell','hi','bye'}

    A =
     [hi] [bye] [good] [tell] [hi] [bye]

    --> unique(A)

    ans =
     [bye] [good] [hi] [tell]