Keep only certains rows (continuation)

This is a follow-up question of this one.
I have the following matrix:
A = [1 2 2;
1 3 3;
1 1 2;
2 9 3;
2 4 3;
2 0 2]
which has an index (1, 2, etc...) in the first column, and the relevant information in the 2nd column; as well as another third column that contains more information.
I would like to reduce this matrix so that only 1 row stays with the same index: the row with the minimum relevant value (2nd column). The third column values of the remaining rows should be kept.
It should look like this:
B = [1 1 2;
2 0 2]
the rest of the rows should not appear.
Is there an easy way I could do that?

Réponses (2)

Roger Stafford
Roger Stafford le 29 Juin 2013
A variation on Matt's method which takes advantage of the fact that column 1 in B is already sorted.
B = sortrows(A,[1,2]);
B = B(find([true;diff(B(:,1))>0]),:);
Matt J
Matt J le 29 Juin 2013
Modifié(e) : Matt J le 29 Juin 2013
B=sortrows(A,[1,2]);
[~,idx]=unique(B(:,1),'stable');
B=B(idx,:)

Catégories

En savoir plus sur Matrices and Arrays dans Centre d'aide et File Exchange

Question posée :

le 29 Juin 2013

Community Treasure Hunt

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

Start Hunting!

Translated by