help to find similar variables and add them togeher
2 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Teja Muppirala
2011-6-14
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')]
0 个评论
更多回答(1 个)
Gerd
2011-6-14
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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!