loop subsampling a timetable for each year for annual max values

조회 수: 6 (최근 30일)
NOtway
NOtway 2023년 5월 27일
댓글: Peter Perkins 2023년 6월 5일
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.

채택된 답변

NOtway
NOtway 2023년 5월 31일
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
Peter Perkins
Peter Perkins 2023년 6월 5일
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.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by