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

조회 수: 3 (최근 30일)
Alberto Acri
Alberto Acri 2023년 11월 3일
편집: Jon 2023년 11월 3일
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일
This is a neat approach. I haven't used splitapply very much, but I can see here how powerful it is.

댓글을 달려면 로그인하십시오.

추가 답변 (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에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by