Weekly Average per Year For Loop

조회 수: 1 (최근 30일)
Emma Richardson
Emma Richardson 2019년 11월 16일
댓글: dpb 2019년 11월 17일
I am currently working on a project with MODIS LST data. I have 18 years worth of daily data, although not all of it is good enough quality to use.
I am needing to set up a for loop which will generate a weekly average LST value - however this value needs to be derived from first averaging within the weeks i.e. an average of week 1 for year 1, week 2 for year 1, week 3 for year 1 and then generating an overall average for week 1 - i.e. by averaging the week 1 value for year 1 with the week 1 value for year 2 all the way to year 18. Eventually I am seeking an 52 row output which has the average for week 1-52 for the 18 years of data.
Due to the quality control issues I have generated a logical called RENLSTdayQC which selects which data is appropriate to use.
This is my current code however it is averaging all week 1 individual day values in the central for loop not the individual years. Any advice would be much appreciated!
n = 0
for i = 1:18 % number of years
use = and(RenLSTdayQC, RenMODISYear == i);
for j=1:52 % number of weeks per year
n= n+1
use2 = and(RenLSTdayQC, RenMODISWeek == j));% look for data of good quality and data in correct year and week
RenLSTWeekAverages(n,1) = nanmean(RenLSTday(use2)); % aiming to output a (939,1) of the average week values for each week for each year
end
RenLST18YAverage(i,1) = nanmean(RenLSTdayWeek(use)); % aiming to output a (52,1) of average week values based on the (939,1) output.
end

답변 (1개)

dpb
dpb 2019년 11월 16일
No loops should be needed.
If you haven't convert the date data to ML datetime and then compute the grouping variable wkyr as
wkyr=week(datevariable);
where datevariable is the date variable name you've chosen to use.
Then compute the average by week using that grouping variable, year, and the quality index
  댓글 수: 1
dpb
dpb 2019년 11월 17일
Acyually, probably just use mod instead of calendar weeks here. NB: days 365/366 will be week 53 unless you want to merge therm

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by