Find the maximum 5 day total in a year

5 vues (au cours des 30 derniers jours)
Eli
Eli le 11 Jan 2023
Commenté : Eli le 11 Jan 2023
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 commentaires
Star Strider
Star Strider le 11 Jan 2023
I do not see any datetime or other time vector associated with ‘P_3’.
Eli
Eli le 11 Jan 2023
Hi @Star Strider, the number of days representing the start and end of each year are represented by the matrices t1 and t2.

Connectez-vous pour commenter.

Réponse acceptée

KSSV
KSSV le 11 Jan 2023
% 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

Plus de réponses (0)

Catégories

En savoir plus sur Data Type Identification dans Help Center et File Exchange

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by