How to identify dulplicates and keep the one with maximum value

5 次查看(过去 30 天)
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 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by