How to create daily average of different consecutive years?

3 ビュー (過去 30 日間)
Wolfgang McCormack
Wolfgang McCormack 2019 年 7 月 24 日
コメント済み: Adam Danz 2019 年 7 月 26 日
Hi, I have a question. I am having a dataset made of 6 consecutive years (Hourly Data). I was able to calculate weekly/daily/annual averages of each year but when it's plotted, it is hard to read. I want to create daily averages that is the average of the day in all 6 years. For instance:
Year Value
Day 1 (2000) 1
Day 1 (2001) 0
Day 1 (2002) 1
Day 1 (2003) 1
Day 1 (2004) 0
Day 1 (2005) 1
The average of day 1 for all 6 years is 0.66
[Data is exactly ordered the same in my dataset file.]
I have turned my imported excel file into time table.

採用された回答

Adam Danz
Adam Danz 2019 年 7 月 24 日
編集済み: Adam Danz 2019 年 7 月 24 日
Assuming your timestamps are in datetime format, use day() with 'dayofyear' flag to convert the timestamps to day-of-year.
Then use findgroups() to group the data by day-of-year. Then use splitapply() to calculate the means.
% Fake timestamp and data, both must be column vectors!
d = datetime('01/01/1950','InputFormat','dd/MM/yyyy') + days(0:3000)';
data = rand(size(d));
doy = day(d,'dayofyear');
[dayGroups, dayKey] = findgroups(doy);
dayMeans = splitapply(@(x)mean(x,'omitnan'),data,dayGroups);
T = table(dayKey,dayMeans,'VariableNames',{'DayOfYear','DayMeans'});
  3 件のコメント
Guillaume
Guillaume 2019 年 7 月 26 日
I agree with everything Adam said,
If you are on R2018a or later, the above can even be achieved in just one line with groupsummary:
result = groupsummary(yourtableortimetable, 'RowTimes', 'dayofyear', 'mean')
Adam Danz
Adam Danz 2019 年 7 月 26 日
Thanks, Guillaume. I keep forgetting about that function.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLine Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by