MATLAB Answers

How to identify dulplicates and keep the one with maximum value

27 ビュー (過去 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,:);
  1 件のコメント
Damith
Damith 2015 年 12 月 16 日
Thanks a lot. It worked.

サインインしてコメントする。

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by