How to create daily average of different consecutive years?

5 visualizaciones (últimos 30 días)
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.

Respuesta aceptada

Adam Danz
Adam Danz el 24 de Jul. de 2019
Editada: Adam Danz el 24 de Jul. de 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 comentarios
Guillaume
Guillaume el 26 de Jul. de 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 el 26 de Jul. de 2019
Thanks, Guillaume. I keep forgetting about that function.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Line Plots en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by