How to create daily average of different consecutive years?

3 Ansichten (letzte 30 Tage)
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.

Akzeptierte Antwort

Adam Danz
Adam Danz am 24 Jul. 2019
Bearbeitet: Adam Danz am 24 Jul. 2019
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 Kommentare
Guillaume
Guillaume am 26 Jul. 2019
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 am 26 Jul. 2019
Thanks, Guillaume. I keep forgetting about that function.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Line Plots finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by