Sum of some elements in two separate vectors

2 次查看(过去 30 天)
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?

采纳的回答

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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by