Remove rows duplicates based on a condition

6 visualizaciones (últimos 30 días)
pink flower
pink flower el 27 de Ag. de 2020
Comentada: pink flower el 1 de Sept. de 2020
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

Respuestas (2)

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

Iniciar sesión para comentar.


Bruno Luong
Bruno Luong el 27 de Ag. de 2020
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 comentario
pink flower
pink flower el 1 de Sept. de 2020
I managed using only the second line! If I want to use the second and third lines as a criterion, is it possible?
Thanks!

Iniciar sesión para comentar.

Categorías

Más información sobre Matrices and Arrays en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by