How to calculate the cumulative value by consequetive hours

조회 수: 1 (최근 30일)
MJ
MJ 2013년 5월 20일
Example I have these values to calculate them every 1 to 5 hours cumulative and continuous;
Data: 1 2 3 4 5 6 7 8
Answer for cumulative every 2 hours will be; 3 5 7 9 11 13 15
Answer for cumulative every 3 hours will be; 6 9 12 15 18 21
Answer for cumulative every 4 hours will be; 10 14 18 22 26
And answer for cumulative every 5 hours will be; 15 20 25 30
Thank you in advance.
  댓글 수: 1
Jan
Jan 2013년 5월 22일
Posting the inputs in valid Matlab syntax has the advantages to be clear and to be usable for suggestions by copy&paste. I cannot guess, what "answer wil be" exactly means.

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

답변 (2개)

Teja Muppirala
Teja Muppirala 2013년 5월 22일
data = [1 2 3 4 5 6 7 8];
for n = 2:5
conv(data,ones(1,n),'valid')
end
This gives me:
ans =
3 5 7 9 11 13 15
ans =
6 9 12 15 18 21
ans =
10 14 18 22 26
ans =
15 20 25 30
  댓글 수: 1
Teja Muppirala
Teja Muppirala 2013년 5월 22일
I am not sure how you plan to store/use these values, but you might make use of cell arrays to store data of unequal lengths.

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


Iain
Iain 2013년 5월 20일
Where n is how many data points you want to do it for:
cumulative = cumsum(Data);
subtract = cumsum([zeros(1,n) Data(1:(end-n))]);
cumulative_n = cululative - subtract;
  댓글 수: 2
MJ
MJ 2013년 5월 22일
I have tried but it only calculated for 1 hour cumulative. 3 6 10 15 21 28 36. How about for 2 hours and so on? That subtract doesnt work.
I use this;
n=8
data=importdata('data.txt')
cumulative = cumsum(data);
subtract = cumsum([zeros(1,n) Data(1:(end-n))]);
cumulative_n = cumulative - subtract;
Iain
Iain 2013년 5월 22일
My apologies. cumulative_n = [cumulative(n) (cumulative(n+1:end)-subtract(n+1:end))];
With 8 data points, and n = 8, you will only get 1 value.

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

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by