Remove rows duplicates based on a condition
조회 수: 5 (최근 30일)
이전 댓글 표시
I have an array with 717136 rows and 5 columns. I need to exclude the rows where the value of the first column is the same, but with one condition: leave only the row where the value of column 5 is greater. My matrix looks like this:
12178 -5,22911 -37,2923 20,5000 3000
12672 -5,17523 -37,2833 21,5000 3000
12178 -5,22911 -37,2923 20,5000 4000
12672 -5,17523 -37,2833 21,5000 4000
12673 -5,18421 -37,2833 21,8799 4000
12674 -5,19319 -37,2833 22,3799 4000
12675 -5,20217 -37,2833 21,1299 4000
12679 -5,23809 -37,2833 20,5000 4000
12673 -5,18421 -37,2833 21,8799 5000
12674 -5,19319 -37,2833 22,3799 5000
12675 -5,20217 -37,2833 21,1299 5000
12679 -5,23809 -37,2833 20,5000 5000
And I want result this:
12178 -5,22911 -37,2923 20,5000 4000
12672 -5,17523 -37,2833 21,5000 4000
12673 -5,18421 -37,2833 21,8799 5000
12674 -5,19319 -37,2833 22,3799 5000
12675 -5,20217 -37,2833 21,1299 5000
12679 -5,23809 -37,2833 20,5000 5000
In other words, the conditions are: if you have more than one row with the same number (eg 12178) in the first column, keep only the row in which the value in the last column is greater. In the case of the example I gave, just keep:
12178 -5,22911 -37,2923 20,5000 4000
12672 -5,17523 -37,2833 21,5000 4000
12673 -5,18421 -37,2833 21,8799 5000
12674 -5,19319 -37,2833 22,3799 5000
12675 -5,20217 -37,2833 21,1299 5000
12679 -5,23809 -37,2833 20,5000 5000
댓글 수: 0
답변 (2개)
Sean de Wolski
2020년 8월 27일
편집: Sean de Wolski
2020년 8월 27일
MWE- use the 'stable' flag to unique if you care about order:
x = [172 2 1;
134 3 2;
172 4 0.4];
[uv,~,idx] = unique(x(:,1));
mx = accumarray(idx,(1:numel(idx))',[],@(v)v(maxind(x(v, end))));
x(mx,:)
function mx = maxind(x)
[~, mx] = max(x);
end
ans =
134 3 2
172 2 1
Bruno Luong
2020년 8월 27일
A=[ ...
12178 -5.22911 -37.2923 20.5000 3000;
12672 -5.17523 -37.2833 21.5000 3000;
12178 -5.22911 -37.2923 20.5000 4000;
12672 -5.17523 -37.2833 21.5000 4000;
12673 -5.18421 -37.2833 21.8799 4000;
12674 -5.19319 -37.2833 22.3799 4000;
12675 -5.20217 -37.2833 21.1299 4000;
12679 -5.23809 -37.2833 20.5000 4000;
12673 -5.18421 -37.2833 21.8799 5000;
12674 -5.19319 -37.2833 22.3799 5000;
12675 -5.20217 -37.2833 21.1299 5000;
12679 -5.23809 -37.2833 20.5000 5000]
B=sortrows(A,[1 5]);
B=B([diff(B(:,1))~=0; true],:)
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!