How to sum the second position data in an array (50000x2) with the same value in the first position
2 次查看(过去 30 天)
显示 更早的评论
Hello everyone im having some issues trying to solve this. I have an array with this form
A=
2.79314 0.007
2.79314 0.083
2.79314 0.051
2.91205 0.06
2.91205 0.06
2.97143 0.08
2.97143 0.06
2.97143 0.07
2.97143 0.08
What i want is that whenever the data of the first column is the same, sum the data in the second column.This is what I want to obtain:
B=
2.79314 (0.007+0.083+0.051)
2.91205 (0.06+0.06+0.08)
2.97143 (0.08+0.06+0.07+0.08)
Thnak you very much in advance.
Regards.
0 个评论
采纳的回答
Stephen23
2020-6-3
编辑:Stephen23
2020-6-3
Method one: accumarray:
>> [U,~,G] = uniquetol(A(:,1));
>> S = accumarray(G(:),A(:,2));
>> M = [U,S]
M =
2.79314 0.14100
2.91205 0.12000
2.97143 0.29000
Mehod two: splitapply:
>> S = splitapply(@sum,A(:,2),G);
>> M = [U,S]
M =
2.7931 0.1410
2.9120 0.1200
2.9714 0.2900
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!