Filter löschen
Filter löschen

Find the maximum 5 day total in a year

1 Ansicht (letzte 30 Tage)
Eli
Eli am 11 Jan. 2023
Kommentiert: Eli am 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 Kommentare
Star Strider
Star Strider am 11 Jan. 2023
I do not see any datetime or other time vector associated with ‘P_3’.
Eli
Eli am 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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

KSSV
KSSV am 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

Weitere Antworten (0)

Kategorien

Mehr zu Dates and Time finden Sie in Help Center und File Exchange

Produkte


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by