Creating daily average of data set from half hourly data

5 vues (au cours des 30 derniers jours)
Sophie Stringer
Sophie Stringer le 26 Avr 2019
Commenté : Sophie Stringer le 27 Avr 2019
Hi there,
I have a data set of variables over 92 days of half hourly recordings. So I have 4,416 data points (48*92 - 48 half hour periods in 1 day)
Instead of having 4,416 data points, I want to create a daily average. So I instead only have 92 averages (or 92 days).
So essential I want to create an average of data points for every 48 data points (1 day)
I want to use a loop to do this, and this is what I have so far, but don't believe its giving me the correct results.
Any help would be much appreciated!
Thank you :D
for i=1:92
index=i:48:(92*48);
data_daily(i)=nanmean(data(index));
end

Réponse acceptée

Adam Danz
Adam Danz le 26 Avr 2019
Modifié(e) : Adam Danz le 26 Avr 2019
Option 1
The cleanest solution (IMHO) is to convert your data to a timetable and use retime() to calculate the daily average. No loops needed.
Option 2
If you can't work with timetables and your data are faithfully sampled 48 times per day,
data = rand(4416,1); %fake data
nDays = 92;
nSamples = 48;
dayIdx = repelem((1:nDays)', nSamples, 1);
dailyAvg = splitapply(@nanmean, data, dayIdx);
Option 3
If you must use a for-loop for whatever reason and your data are faithfully sampled 48 times per day,
data = rand(4416,1); %fake data
nDays = 92;
nSamples = 48;
dayIdx = repelem((1:nDays)', nSamples, 1);
dailyAvg = zeros(nDays, 1);
for i = 1:nDays
dailyAvg(i) = mean(data(dayIdx==i),'omitnan');
end
Option 4
If your data are not faithfully sampled 48 times per day, and if you have a vector of time stamps, you can use those time stamps to identify the day and then use findgroups() to create the 'dayIdx' vector in my options 2 and 3.
  1 commentaire
Sophie Stringer
Sophie Stringer le 27 Avr 2019
I went with the loops and it worked!
Thanks a lot!
Cheers :D

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by