select rows based on the value of the last column of the matrix

3 visualizzazioni (ultimi 30 giorni)
I have a matrix 'A'. As can be seen in the figure (red rectangles), some rows have the first and second columns the same but not the third column. Considering one of the red rectangles, I would like to select the entire row (green rectangle) that has a greater value in the fourth column. The end result is the matrix 'A_out'.
A = [334 1358 175 3
335 1359 176 3
335 1359 175 1
336 1360 176 2
337 1361 176 6
337 1361 177 1
338 1362 177 5
339 1363 177 10
340 1364 178 1
340 1364 177 10];
A_out = [334 1358 175
335 1359 176
336 1360 176
337 1361 176
338 1362 177
339 1363 177
340 1364 177];

Risposta accettata

Matt J
Matt J il 3 Nov 2023
Modificato: Matt J il 3 Nov 2023
This solution requires only 1 sorting operation.
A = [334 1358 175 3
335 1359 176 3
335 1359 175 1
336 1360 176 2
337 1361 176 6
337 1361 177 1
338 1362 177 5
339 1363 177 10
340 1364 178 1
340 1364 177 10];
B=sortrows(A,[1,2,4]);
I=diff([findgroups(B(:,1), B(:,2));0])~=0;
A_out=B(I,1:3)
A_out = 7×3
334 1358 175 335 1359 176 336 1360 176 337 1361 176 338 1362 177 339 1363 177 340 1364 177
  1 Commento
Jon
Jon il 3 Nov 2023
This is a neat approach. I haven't used splitapply very much, but I can see here how powerful it is.

Accedi per commentare.

Più risposte (1)

Jon
Jon il 3 Nov 2023
Modificato: Jon il 3 Nov 2023
A = [334 1358 175 3
335 1359 176 3
335 1359 175 1
336 1360 176 2
337 1361 176 6
337 1361 177 1
338 1362 177 5
339 1363 177 10
340 1364 178 1
340 1364 177 10];
% Sort by first and then third column
% use descending order so that when first column values are the same
% rows with bigger 3rd column values
% will occur first
Asrt = sortrows(A,[1,3],"descend")
Asrt = 10×4
340 1364 178 1 340 1364 177 10 339 1363 177 10 338 1362 177 5 337 1361 177 1 337 1361 176 6 336 1360 176 2 335 1359 176 3 335 1359 175 1 334 1358 175 3
% Get location of first instance of each unique first column value (by default sorted
% in increasing order), that is location for 334,335,336...
[~,ia] = unique(Asrt(:,1))
ia = 7×1
10 8 7 5 4 3 1
% Choose rows with largest 3rd column value
A_out = Asrt(ia,(1:3))
A_out = 7×3
334 1358 175 335 1359 176 336 1360 176 337 1361 177 338 1362 177 339 1363 177 340 1364 178
  2 Commenti
Voss
Voss il 3 Nov 2023
I like this answer, but I think to get the OP's intent ("first and second columns the same ... select the entire row that has a greater value in the fourth column"), a couple of modifications are needed:
A = [334 1358 175 3
335 1359 176 3
335 1359 175 1
336 1360 176 2
337 1361 176 6
337 1361 177 1
338 1362 177 5
339 1363 177 10
340 1364 178 1
340 1364 177 10];
Asrt = sortrows(A,[1 2 4],"descend"); % sort by column 1, then 2, then 4
[~,ia] = unique(Asrt(:,[1 2]),'rows'); % unique according to first two columns
A_out = Asrt(ia,1:3)
A_out = 7×3
334 1358 175 335 1359 176 336 1360 176 337 1361 176 338 1362 177 339 1363 177 340 1364 177
Jon
Jon il 3 Nov 2023
Modificato: Jon il 3 Nov 2023
@Voss, Oh, I see now that in the OP's written description they say to select the "entire row", but in the example output A_out, they only show the first three columns. I was just looking at the example. Anyhow the OP can now see how to do it either way.
Oops, you're right my code selected the one with the larger value in the third column, I didn't read the problem description carefully enough.
Also, good catch on the first two columns being unique rather than just the first as I had. For this example I don't think it makes any difference, but your correction follows the OP's specification, thanks.

Accedi per commentare.

Prodotti


Release

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by