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:
- Introduce condition that if the year is a leap year, I should only estimate until 365 days. 366 days is not divisible by 5.
- Sum every 5 days in a year.
- 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 个评论
采纳的回答
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Dates and Time 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!