help to find similar variables and add them togeher

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

Accepted Answer

Teja Muppirala
Teja Muppirala on 14 Jun 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')]

More Answers (1)

Gerd
Gerd on 14 Jun 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

Community Treasure Hunt

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

Start Hunting!