help to find similar variables and add them togeher

1 vue (au cours des 30 derniers jours)
HU Jianho
HU Jianho le 14 Juin 2011
I have a matrix contains two vectors the first row is time vector in nanosecond and the second row is power vector in micro-watt
X=[0.1 0.3 0.2 0.1 0.4 0.2 0.5 0.9 0.7 0.1 0.4 0.8 0.25 0.2 0.3 0.1; 2 3 5 4 7 13 11 5 12 13 12 14 21 2 17 33]
So the time vector is:
t=[0.1 0.3 0.2 0.1 0.4 0.2 0.5 0.9 0.7 0.1 0.4 0.8 0.25 0.2 0.3 0.1]
and the power vector is:
P=[2 3 5 4 7 13 11 5 12 13 12 14 21 2 17 33]
what I want to do is to find the similar time and add their power together.
for example at the time 0.1 ns we received 2 microwatt then again received 4 mw (in fourth column), 13 mw(in tenth column) and 33 mw (in the last column)
Could you please help me how do this as I have thousands of elements and I need to sort them.
thanks in-advance

Réponse acceptée

Teja Muppirala
Teja Muppirala le 14 Juin 2011
If you have the Statistics Toolbox, you can use GRPSTATS:
t=[0.1 0.3 0.2 0.1 0.4 0.2 0.5 0.9 0.7 0.1 0.4 0.8 0.25 0.2 0.3 0.1];
P=[2 3 5 4 7 13 11 5 12 13 12 14 21 2 17 33];
[unique(t)' grpstats(P,t,'sum')]

Plus de réponses (1)

Gerd
Gerd le 14 Juin 2011
Hi, without the Statistics Toolbox I would use
t=[0.1 0.3 0.2 0.1 0.4 0.2 0.5 0.9 0.7 0.1 0.4 0.8 0.25 0.2 0.3 0.1];
P=[2 3 5 4 7 13 11 5 12 13 12 14 21 2 17 33];
difValues = unique(t);
amount = zeros(numel(difValues),1);
for i=1:numel(difValues)
amount(i) = sum(P(t==difValues(i)));
end
My result is 52 20 21 20 19 11 12 14 5
Cheers
  1 commentaire
HU Jianho
HU Jianho le 15 Juin 2011
Thanks Gerd for you answer it does what I needed

Connectez-vous pour commenter.

Catégories

En savoir plus sur Mathematics dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by