help to find similar variables and add them togeher

조회 수: 1 (최근 30일)
HU Jianho
HU Jianho 2011년 6월 14일
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

채택된 답변

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

추가 답변 (1개)

Gerd
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
  댓글 수: 1
HU Jianho
HU Jianho 2011년 6월 15일
Thanks Gerd for you answer it does what I needed

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Mathematics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by