필터 지우기
필터 지우기

Find the maximum 5 day total in a year

조회 수: 2 (최근 30일)
Eli
Eli 2023년 1월 11일
댓글: Eli 2023년 1월 11일
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에서 Data Type Identification에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by