MATLAB Answers

Weekly Average per Year For Loop

1 view (last 30 days)
Emma Richardson
Emma Richardson on 16 Nov 2019
Commented: dpb on 17 Nov 2019
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
RenLST18YAverage(i,1) = nanmean(RenLSTdayWeek(use)); % aiming to output a (52,1) of average week values based on the (939,1) output.


Sign in to comment.

Answers (1)

dpb on 16 Nov 2019
No loops should be needed.
If you haven't convert the date data to ML datetime and then compute the grouping variable wkyr as
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 Comment

dpb on 17 Nov 2019
Acyually, probably just use mod instead of calendar weeks here. NB: days 365/366 will be week 53 unless you want to merge therm

Sign in to comment.

Sign in to answer this question.