フィルターのクリア

Average columns together that share a common value

1 回表示 (過去 30 日間)
Robert
Robert 2023 年 5 月 21 日
回答済み: Paul 2023 年 5 月 21 日
Lets say I have a 2 x 10 matrix M
The first column being an ID, and the second a value
1, 25
3, 33
3. 45
2, 22
4, 54
4, 56
5, 23
5, 65
1, 27
2, 29
How could I average the values with the same ID such that the new matrix would be
1, 26
2, 25.5
3, 39
4, 55
5, 44

回答 (1 件)

Paul
Paul 2023 年 5 月 21 日
D = [1, 25
3, 33
3. 45
2, 22
4, 54
4, 56
5, 23
5, 65
1, 27
2, 29];
[G,ID] = findgroups(D(:,1));
[ID splitapply(@mean,D(:,2),G)]
ans = 5×2
1.0000 26.0000 2.0000 25.5000 3.0000 39.0000 4.0000 55.0000 5.0000 44.0000

カテゴリ

Help Center および File ExchangeOperating on Diagonal Matrices についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by