Supriyo
Supriyo

Reputation: 113

How to get distinct rows of a matrix in matlab

I have a big matrix and MATLAB R2012b. I like to get distinct rows of the matrix with their frequencies. How to do it?

In addition let us think two rows with same entries are equal i.e. $(0 , 0 , 1)$ and $(0 , 1 , 0)$ are equivalent. Then how to get number of distinct rows and their frequencies?

I was trying with the function sortrows(). But it is not very efficient. Also I do not want them to count manually.

Upvotes: 0

Views: 114

Answers (1)

Luis Mendo
Luis Mendo

Reputation: 112779

Let's define an example matrix:

A = [ 0 1 0;
      1 0 0;
      0 1 0;
      1 1 1];
  1. If rows with a different order should be considered as distinct: use unique(...,'rows') to get unique row labels, and histc to do the counting:

    [~, jj, kk] = unique(A,'rows');
    unique_rows = A(jj,:);
    count = histc(kk, unique(kk));
    

    The jj ouput of unique contains the indices of unique rows of A. The kk output is a label that tells, for each row of A, which of the unique rows given by jj it equals. In other words, A(jj(kk),:) reproduces A (but using only the set of unique rows given by jj).

    The code gives

    unique_rows =
         0     1     0
         1     0     0
         1     1     1
    
    count =
         2
         1
         1
    
  2. If rows should be considered equal even if they are in a different order: just sort each row before calling unique(...,'rows'):

    [~, jj, kk] = unique(sort(A,2),'rows');
    unique_rows = A(jj,:);
    count = histc(kk, unique(kk));
    

    This gives

    unique_rows =
         0     1     0
         1     1     1
    
    count =
         3
         1
    

Upvotes: 3

Related Questions