Find the maximum 5 day total in a year

2 次查看(过去 30 天)
Dear all,
I have data from 1992-2020. For each year, I want to find the maximum 5 day total.
Steps taken:
  1. Introduce condition that if the year is a leap year, I should only estimate until 365 days. 366 days is not divisible by 5.
  2. Sum every 5 days in a year.
  3. Find the maximum sum in a year.
load('Data.mat');
% % Case A: What I want in simpler terms.
a = P_3(1:366);
b = P_3(367:731);
if length(a) == 366
for i = 1:5:length(a)-1
s1(i) = sum(a(i:i+4));
end
end
if length(b) == 365
for i = 1:5:length(b)
s2(i) = sum(b(i:i+4));
end
end
s1a = max(s1);
s2a = max(s2);
% % Case B: What I have tried to implement.
y3 = P_3;
for j = 1:size(t1,2)
a = y3(t1(j):t2(j));
if length(a) == 366
for k = 1:5:length(a)-1
sa1(k,:) = max(sum(a(k:k+4)));
end
else
for k = 1:5:length(a)
sa1(k,:) = max(sum(a(k:k+4)));
end
end
end
sa2 = max(sa1);
However, case B does not give me the desired results. It only returns the maximum 5 day total of the last year, not all years. How do I resolve this problem? Is it also possible to replicate the same thing without using for loops and if statement?
Thank you very much.
  2 个评论
Star Strider
Star Strider 2023-1-11
I do not see any datetime or other time vector associated with ‘P_3’.
Eli
Eli 2023-1-11
Hi @Star Strider, the number of days representing the start and end of each year are represented by the matrices t1 and t2.

请先登录,再进行评论。

采纳的回答

KSSV
KSSV 2023-1-11
% create time stamps from 1992 to 2020
thedates = (datetime(1992,1,1):days(1):datetime(2020,12,31))' ;
idx = thedates.Day==29 & thedates.Month==2 ; % get the logicaL indices of 29th day of Feb in leap years
% Remove the Feb 29 the values from the data
data = P_3 ;
data(idx) = [] ;
% reshape the data to each year
data = reshape(data,[],length(data)/365) ;
% sum for every five days for each year by reshaping
[r,c] = size(data);
nlay = 365/5;
out = permute(reshape(data',[c,r/nlay,nlay]),[2,1,3]);
thesum = squeeze(sum(out,1)) ;
iwant = max(thesum,[],2) % get the maximum

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Dates and Time 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by