loop subsampling a timetable for each year for annual max values

6 views (last 30 days)
I have 9 different daily-interval timetable datasets, each spanning ~100 years of data. I need to extract the maximum annual 5-day sum of variable values for each year, in each dataset.
I've figured out how I can get the maximum 5-day sum across the entire 100 year dataset:
v = Timetable_1of9.Var1 ;
n = 5 ;
N = length(v) ;
sumn = zeros(1, N - n + 1); % Pre-allocation
for i = 1:N - n + 1
sumn(i) = sum(v(i:(i+n-1))) ;
end
[val,idx] = max(sumn)
but I'm not sure of the most efficient way to do this for each year and each dataset. (I also need those values put back into 9 new timetables with each annual maximum assigned to 1-1-YYYY, to match in with later analysis I will be doing).
If there a way I can set up a batch or a another for loop to run through each? even if it just does 1 dataset at a time, I can easily copy the code 8 more times and change the input file. Doesn't need to be pretty, just needs to work.

Accepted Answer

NOtway
NOtway on 31 May 2023
If anyone in the future is looking for an anser: I wrked it out myself. Its definitely not the most elegant solution, but it works so good enough for me
%% sum each 5 days from 1-Jan-2015
Aggregate_5day_1 = retime(Aggregate_1day, 'regular', 'sum', 'TimeStep', caldays(5));
% find annual max of these values
Aggregate_5day_1_max = retime(Aggregate_5day_1,"yearly", "max");
%% sum each 5 days from 2-Jan-2015
% delete 1-Jan-2015 from Aggregate_1day timetable
Aggregate_1day(1,:) = [];
Aggregate_5day_2 = retime(Aggregate_1day, 'regular', 'sum', 'TimeStep', caldays(5));
% find annual max of these values
Aggregate_5day_2_max = retime(Aggregate_5day_2,"yearly", "max");
%% sum each 5 days from 3-Jan-2015
% delete 2-Jan-2015 from Aggregate_1day timetable
Aggregate_1day(1,:) = [];
Aggregate_5day_3 = retime(Aggregate_1day, 'regular', 'sum', 'TimeStep', caldays(5));
% find annual max of these values
Aggregate_5day_3_max = retime(Aggregate_5day_3,"yearly", "max");
%% sum each 5 days from 4-Jan-2015
% delete 3-Jan-2015 from Aggregate_1day timetable
Aggregate_1day(1,:) = [];
Aggregate_5day_4 = retime(Aggregate_1day, 'regular', 'sum', 'TimeStep', caldays(5));
% find annual max of these values
Aggregate_5day_4_max = retime(Aggregate_5day_4,"yearly", "max");
%% sum each 5 days from 5-Jan-2015
% delete 4-Jan-2015 from Aggregate_1day timetable
Aggregate_1day(1,:) = [];
Aggregate_5day_5 = retime(Aggregate_1day, 'regular', 'sum', 'TimeStep', caldays(5));
% find annual max of these values
Aggregate_5day_5_max = retime(Aggregate_5day_5,"yearly", "max");
%% Find annual total max from the max of those 5 values for each year
Aggregate_5day_temp = synchronize(Aggregate_5day_1_max, Aggregate_5day_2_max, Aggregate_5day_3_max, Aggregate_5day_4_max, Aggregate_5day_5_max);
Wet3_7_5day = timetable(Aggregate_5day_temp.Time,Aggregate_5day_temp.Wet3_7_Aggregate_5day_1_max, Aggregate_5day_temp.Wet3_7_Aggregate_5day_2_max, ...
Aggregate_5day_temp.Wet3_7_Aggregate_5day_3_max, Aggregate_5day_temp.Wet3_7_Aggregate_5day_4_max, Aggregate_5day_temp.Wet3_7_Aggregate_5day_5_max);
Wet3_7_5day_max_temp = max([Wet3_7_5day.Var1, Wet3_7_5day.Var2, Wet3_7_5day.Var3, Wet3_7_5day.Var4, Wet3_7_5day.Var5], [], 2);
Wet3_7_5day_max = array2timetable(Wet3_7_5day_max_temp, "RowTimes", Wet3_7_5day.Time);
  1 Comment
Peter Perkins
Peter Perkins on 5 Jun 2023
Yes, this would be simple if it was "non-overlapping 5-day windows".
For what it's worth I think you could add 1-day,...4-day lagged versions of your data to the timetable and do the "five day max" part for the different lags all at once, and then collapse with a max across the different lags. Less code, anyway.

Sign in to comment.

More Answers (0)

Categories

Find more on Dates and Time in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!