Remove rows duplicates based on a condition

4 次查看(过去 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

回答(2 个)

Sean de Wolski
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
  1 个评论
pink flower
pink flower 2020-9-1
I tried these commands but got this error:
Undefined function or variable 'maxind'.
Error in @(v)v(maxind(x(v,end)))

请先登录,再进行评论。


Bruno Luong
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],:)
  1 个评论
pink flower
pink flower 2020-9-1
I managed using only the second line! If I want to use the second and third lines as a criterion, is it possible?
Thanks!

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by