How to identify dulplicates and keep the one with maximum value
20 views (last 30 days)
Show older comments
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/152675/image.bmp)
0 Comments
Accepted Answer
the cyclist
on 16 Dec 2015
% 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,:);
More Answers (0)
See Also
Categories
Find more on Shifting and Sorting Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!