필터 지우기
필터 지우기

Select specified (max) values

조회 수: 1 (최근 30일)
gianluca
gianluca 2017년 2월 12일
편집: Jan 2017년 2월 12일
Dear all, I've a matrix, e.g.:
A = [2 1 1 1440 62 2 6 8; 2 2 1 1882 65 2 7 8; 3 1 1 820 51 2 5 8; 3 2 1 1435 65 2 6 8; 3 3 1 1608 67 2 6 8; 9 1 1 394 38 1 2 8;9 2 1 2198 93 2 9 8; 9 2 2 2198 91 2 12 8; 9 2 3 2198 92 2 16 8; 9 3 1 2994 122 1 7 8; 9 3 2 2994 125 1 10 8; 9 3 3 2994 126 1 14 8];
For each row with the same value in the 1st and 2nd column, I've to take only the row where the 3th column is the maximum. I would, as result, a matrix like this:
B = [2 1 1 1440 62 2 6 8; 2 2 1 1882 65 2 7 8; 3 1 1 820 51 2 5 8; 3 2 1 1435 65 2 6 8; 3 3 1 1608 67 2 6 8; 9 1 1 394 38 1 2 8; 9 2 3 2198 92 2 16 8; 9 3 3 2994 126 1 14 8];
[key, ~, index] = unique(A(:,[1 2]),'stable','rows');
x = accumarray(index, A(:,3), [], @max);
...and I'm blocked here!

채택된 답변

Jan
Jan 2017년 2월 12일
편집: Jan 2017년 2월 12일
You can start with sorting the array according to the first 3 columns. Then take the rows which contain a change in the 1st or 2nd column, and the last row:
S = sortrows(A, 1:3); % Largest 3rd col to bottom
Index = [diff(S(:, 1)) | diff(S(:, 2)); true]; % 1st or 2nd col changes
B = A(Index, :) % Extract result

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by