Creating daily average of data set from half hourly data

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

Akzeptierte Antwort

Adam Danz
Adam Danz am 26 Apr. 2019
Bearbeitet: Adam Danz am 26 Apr. 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.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by