Keep only certains rows (continuation)
이전 댓글 표시
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
2013년 6월 29일
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]),:);
B=sortrows(A,[1,2]);
[~,idx]=unique(B(:,1),'stable');
B=B(idx,:)
카테고리
도움말 센터 및 File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!