Weekly Average per Year For Loop

4 次查看(过去 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