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 CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by