How to sum the second position data in an array (50000x2) with the same value in the first position

1 vue (au cours des 30 derniers jours)
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.

Réponse acceptée

Stephen23
Stephen23 le 3 Juin 2020
Modifié(e) : Stephen23 le 3 Juin 2020
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

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Tags

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by