Sum of some elements in two separate vectors

1 view (last 30 days)
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?

Accepted Answer

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

More Answers (0)

Categories

Find more on Numeric Types in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!