# Calculate average for each hour in day

28 visualizzazioni (ultimi 30 giorni)
Oliver Zacho il 20 Giu 2020
Risposto: Catriona Fyffe il 22 Lug 2020
I have a bunch of data in datetime format similar to this:
2010-Nov-30 18:00.00 0 8.7 17.05 61.38
I have summed all the data into hours, but I would like to calculate the average at every hour 00:00, 01:00, 02:00... 23:00 for every day in the year.
I have data for a whole year, so I'm interested in seeing the average values in a day, per hour, with the data from the whole year.
I've tried to use retime but I can't really get it to function correctly. Maybe it's the wrong function for this purpose.
##### 3 CommentiMostra 1 commento meno recenteNascondi 1 commento meno recente
Oliver Zacho il 20 Giu 2020
I can't get it to group all the hours from the whole year into on matrix that's 24-by-x with the mean from every hour.
Do you have a hint? :)
Adam Danz il 20 Giu 2020
Could you show us what you tried and describe the input variables used? It will be easier to help you tweek that instead of creating a different example out of our imaginations that may not even be suitable with your data.

Accedi per commentare.

### Risposta accettata

dpb il 20 Giu 2020
IIUC, retime is not the tool for this; it bins data into desired time segments such as hourly but averages within those bins.
To compute an average of all observations in the dataset at each exact hour, use a grouping variable and varfun or groupsummary or grpsummary (latter requires Statistics Toolbox and has essentially been replaced by former in base MATLAB).
You will have to group by sufficiently-fine resolution to isolate the one actual top-oif-the-hour reading by hours(), minutes(), seconds(), ... depending upon the time-of-day readings in the dataset.
##### 6 CommentiMostra 4 commenti meno recentiNascondi 4 commenti meno recenti
Oliver Zacho il 20 Giu 2020
Ohhh I see! Thanks for pointing that out!
My solution is now simply to set the date to 0 when I'm making my datetime.
t = datetime(0,0,0,dataAR(:,4),dataAR(:,5),dataAR(:,6),'Format','HH:mm:ss');
This seems to do the job!
Thanks alot for the explanation!
Have a good evening (depending on your timezone) :)
dpb il 20 Giu 2020
The better solution is probably to just use hour() of the real datetime -- that will return a double from 0-23 for each.
Since you have only the one hourly observation, you won't need the retime step.

Accedi per commentare.

### Più risposte (1)

Catriona Fyffe il 22 Lug 2020
A very simple way to solve this if you have whole days starting at 00:00 is to use reshape. Its a bit old school but I like it! However I have come onto this page to solve this problem for data which doesn't start at 00:00 (not my choice!)
NHours=size(VarA,1);
NDays=NHours/24;
VarA_AH=nanmean(reshape(VarA,[24,NDays]),2);
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Categorie

Scopri di più su Tables in Help Center e File Exchange

R2020a

### Community Treasure Hunt

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

Start Hunting!

Translated by