Sum of some elements in two separate vectors
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Amin Gan
am 29 Okt. 2015
Kommentiert: Amin Gan
am 29 Okt. 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?
0 Kommentare
Akzeptierte Antwort
Thorsten
am 29 Okt. 2015
[uA a b] = unique(A);
sB = arrayfun(@(x) (sum(B(b==x))), 1:numel(a));
X = [uA sB'];
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Resizing and Reshaping Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!