MATLAB Answers

Moving sum in a timetable

9 views (last 30 days)
Hi,
I've timetable with 2 clomuns (time column) 527040×1 datetime and 527040×1 double (1st column) , please look at my attached screenshot :)
the datetime is on a minutley basis from 01.01.2018 00:00:00 until 01.01.2019 23:59:00
I want to calculte the moving sum or sum of the double column for t0 = sum over the next 5 minutes startting at t1
So it should be value at t0 = sum of of double from t1+...+t6
for value at t1 = sum t2+...+t7
and so on.....
any ideas :)

Accepted Answer

Bob Thompson
Bob Thompson on 9 Apr 2019
Edited: Bob Thompson on 9 Apr 2019
I'm a little bit confused what you're asking for, but here is a first attempt. As I understand you are looking for each row to have a sum of the next several doubles. That would be done something like:
for i = 1:size(data,1) % I don't know what your data variable is called, replace as needed
if i+5<=size(data,1)
data(i,3) = sum(data(i+1:i+5,2));
elseif i+1<=size(data,1)
data(i,3) = sum(data(i+1:end,2));
else
data(i,3) = NaN;
end
end
I do not, off the top of my head, know a way to calculate a moving some without a loop in Matlab.
  5 Comments
Steven Lord
Steven Lord on 10 Apr 2019
The limitation of this ability, however, is that the range values must be positive integers, so it is not possible to examine values exclusively in front of the summation index, as including 0 as the kb term will begin the summation at the current index.
If you specify datetime or duration as the value for the SamplePoints name-value pair argument, the window length values may be nonnegative integer values or may be duration values. So for this scenario using the timetable RowTimes as the SamplePoints in movsum would allow you to specify minutes([5 0]) as the window length input.
You are correct that you can't take the moving sum (or use any of the moving functions) and exclude the element around which the window is located. But for purposes of movsum that just leaves one post-movsum step. Use fillmissing or isnan to replace any NaN values in A with 0 if necessary then subtract A from the results of movsum. That should be fairly safe for most data sets.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!