find maximum value for each year in a 2 columns array

I have an array with two columns, column one year (1975-2020) and column two discharge values. I need to find the maximum discharge amount for each year and have them saved in another matrix.
Can you please help me with this?
input:
1975 200
1975 85
.
.
.
2002 110
2002 95
2002 32
...
2020 120
...
desired output
1975 200
...
2002 110
...
2020 120

 採用された回答

Tommy
Tommy 2020 年 4 月 16 日

1 投票

Try this:
[G, y] = findgroups(input(:,1));
output = [y splitapply(@max, input(:,2), G)];

5 件のコメント

Mahtab Moalemi
Mahtab Moalemi 2020 年 4 月 16 日
Thanks but it gives me this error:
Error using splitapply (line 99)
The data variables must have the same number of rows as the vector of group numbers. The group number
vector has 46 row(s), and data variable 1 has 934768 row(s).
Tommy
Tommy 2020 年 4 月 16 日
Do you mind posting the code you ran which gave you this error?
Mahtab Moalemi
Mahtab Moalemi 2020 年 4 月 16 日
[years,groups] = findgroups(year_Q(:,1));
>> output = [years splitapply(@max, year_Q(:,2), groups)];
Tommy
Tommy 2020 年 4 月 16 日
編集済み: Tommy 2020 年 4 月 16 日
Switch the outputs.
[groups, years] = findgroups(year_Q(:,1));
>> output = [years splitapply(@max, year_Q(:,2), groups)];
Mahtab Moalemi
Mahtab Moalemi 2020 年 4 月 16 日
nevermind! sorted it out
Thanks

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeDates and Time についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by