select rows based on the value of the last column of the matrix
2 views (last 30 days)
Show older comments
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];
0 Comments
Accepted Answer
Matt J
on 3 Nov 2023
Edited: Matt J
on 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)
1 Comment
Jon
on 3 Nov 2023
This is a neat approach. I haven't used splitapply very much, but I can see here how powerful it is.
More Answers (1)
Jon
on 3 Nov 2023
Edited: Jon
on 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")
% 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))
% Choose rows with largest 3rd column value
A_out = Asrt(ia,(1:3))
2 Comments
Voss
on 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)
Jon
on 3 Nov 2023
Edited: Jon
on 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.
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!