How to identify dulplicates and keep the one with maximum value

조회 수: 3 (최근 30일)
Damith
Damith 2015년 12월 15일
댓글: Damith 2015년 12월 16일
Hi,
I need to perform the following using MATLAB.
1) Identify duplicate years in 2nd column and keep the one with maximum value with the 3rd column.
For example, (see the image below) since I have two 1961 occurences in column 2, need to remove one 1961 which has a highest corresponsding value in 3rd column (keep row with 5430 and remove 1160)
How can I perform this in MATLAB. Any help is appreciated.
Thanks.

채택된 답변

the cyclist
the cyclist 2015년 12월 16일
% Your data:
c = [ ...
5494300 1905 0; ...
5494300 1946 0; ...
5494300 1953 1640; ...
5494300 1954 1060; ...
5494300 1955 2360; ...
5494300 1956 827; ...
5494300 1957 913; ...
5494300 1958 3970; ...
5494300 1959 5660; ...
5494300 1960 8600; ...
5494300 1961 1160; ...
5494300 1961 5430; ...
];
% Define d such that it is sorted by years, and then by the third-column value.
% (You did not mention anything about column 1, so it just goes along for the ride)
d = sortrows(c, [2 3]);
% Find the index to the unique years, getting the last instance in each case,
% because that is where the largest third-column value is (because we sorted).
[~,uniqueYearIndex] = unique(d(:,2),'last');
% Take only those rows
d = d(uniqueYearIndex,:);

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by