Remove rows duplicates based on a condition

6 visualizzazioni (ultimi 30 giorni)
pink flower
pink flower il 27 Ago 2020
Commentato: pink flower il 1 Set 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

Risposte (2)

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

Accedi per commentare.


Bruno Luong
Bruno Luong il 27 Ago 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 Commento
pink flower
pink flower il 1 Set 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!

Accedi per commentare.

Categorie

Scopri di più su Matrices and Arrays in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by