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

2 次查看(过去 30 天)
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];

采纳的回答

Matt J
Matt J 2023-11-3
编辑:Matt J 2023-11-3
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 个)

Jon
Jon 2023-11-3
编辑:Jon 2023-11-3
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 个评论
Voss
Voss 2023-11-3
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 2023-11-3
编辑:Jon 2023-11-3
@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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by