MATLAB Answers

0

Weekly Average per Year For Loop

Emma Richardson さんによって質問されました 2019 年 11 月 16 日 10:38
最新アクティビティ dpb
さんによって コメントされました 2019 年 11 月 17 日 16:03
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

  0 件のコメント

サインイン to comment.

タグ

1 件の回答

dpb
回答者: dpb
2019 年 11 月 16 日 14:27

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
2019 年 11 月 17 日 16:03
Acyually, probably just use mod instead of calendar weeks here. NB: days 365/366 will be week 53 unless you want to merge therm

サインイン to comment.



Translated by