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?

답변 (2개)

Roger Stafford
Roger Stafford 2013년 6월 29일

1 개 추천

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 2013년 6월 29일
편집: Matt J 2013년 6월 29일

0 개 추천

B=sortrows(A,[1,2]);
[~,idx]=unique(B(:,1),'stable');
B=B(idx,:)

카테고리

도움말 센터File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

질문:

2013년 6월 29일

Community Treasure Hunt

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

Start Hunting!

Translated by