Average columns together that share a common value
2 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(1 个)
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)]
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!