MATLAB: Moving sum in a timetable

MATLAB and Simulink Student Suitetimetable sum movsum indexing

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 🙂

Best Answer

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.
Related Question