How to quickly find the maximum group of same rows in a matrix?

1 vue (au cours des 30 derniers jours)
Benson Gou
Benson Gou le 18 Nov 2019
Commenté : Star Strider le 2 Déc 2019
Dear All,
I have a matrix and there exist identical rows in the matrix. I want to find the maximum group of identical rows in this matrix. For example, I have a matrix as follows:
A = [0 1 0 1 0 0 0 0; 1 0 0 0 1 0 0 0; 1 0 0 0 1 0 0 0; 1 1 0 0 0 0 0 0; 0 0 1 0 0 0 0 1; 0 0 0 0 0 1 1 0; 0 1 0 1 0 0 0 0; 0 1 0 1 0 0 0 0].
There are two groups of identical rows: {row 1, row 7 and row 8}, {row 2, row 3}. So the answer is {row 1, row 7, row 8}. But how can I quickly find it?
Best regsards,
Benson

Réponse acceptée

Star Strider
Star Strider le 18 Nov 2019
Try this:
A = [0 1 0 1 0 0 0 0; 1 0 0 0 1 0 0 0; 1 0 0 0 1 0 0 0; 1 1 0 0 0 0 0 0; 0 0 1 0 0 0 0 1; 0 0 0 0 0 1 1 0; 0 1 0 1 0 0 0 0; 0 1 0 1 0 0 0 0];
[Au,~,idx] = unique(A, 'rows');
tally = accumarray(idx, 1);
Out = A(idx == max(tally),:) % The Rows Themselves
RowIdx = find(idx == max(tally)) % The Row Indices
producing:
RowIdx =
1
7
8
  6 commentaires
Benson Gou
Benson Gou le 2 Déc 2019
Dear Star,
Thanks a lot for your great help. Now it works very well.
Happy Holidays!
Bension
Star Strider
Star Strider le 2 Déc 2019
As always, my pleasure!
Happy Holidays to you, too!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Mathematics dans Help Center et File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by