Remove rows duplicates based on a condition

4 Ansichten (letzte 30 Tage)
pink flower
pink flower am 27 Aug. 2020
Kommentiert: pink flower am 1 Sep. 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

Antworten (2)

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

Melden Sie sich an, um zu kommentieren.


Bruno Luong
Bruno Luong am 27 Aug. 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 Kommentar
pink flower
pink flower am 1 Sep. 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!

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by