Sum of some elements in two separate vectors

1 vue (au cours des 30 derniers jours)
Amin Gan
Amin Gan le 29 Oct 2015
Commenté : Amin Gan le 29 Oct 2015
I have two different vectors (1000 numbers),A and B for instance. Each number of A has a specific value in B (A(i,1)=B(i,1)).
Some of A values are repeated. I want to sum the repeated values of A for each value of B and then plot A Vs B. for example:
A=[ 1 ; 2 ; 1 ; 5 ; 10 ; 5 ]
B=[0.1 ; 0.5 ; 0.2 ; 0.3; 0.8 ; 0.9]
For A=1, B=0.1 & 0.2 >>>> so when A=1, B=0.3 (sum of the values) For A=2, B=0.5 >>>> so when A=2, B=0.5 (It has the single value) For A=5, B=0.3 & 0.9 >>>> so when A=5, B=1.2 I have 1000 numbers for each A and B. Can you please help?

Réponse acceptée

Thorsten
Thorsten le 29 Oct 2015
[uA a b] = unique(A);
sB = arrayfun(@(x) (sum(B(b==x))), 1:numel(a));
X = [uA sB'];

Plus de réponses (0)

Catégories

En savoir plus sur Resizing and Reshaping Matrices dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by