# How to identify dulplicates and keep the one with maximum value

17 views (last 30 days)
Damith on 15 Dec 2015
Commented: Damith on 16 Dec 2015
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 on 16 Dec 2015
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,:);
Damith on 16 Dec 2015
Thanks a lot. It worked.