# Creating daily average of data set from half hourly data

7 ビュー (過去 30 日間)
Sophie Stringer 2019 年 4 月 26 日
コメント済み: Sophie Stringer 2019 年 4 月 27 日
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

サインインしてコメントする。

### 採用された回答

Adam Danz 2019 年 4 月 26 日

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 件のコメント表示 なし非表示 なし
Sophie Stringer 2019 年 4 月 27 日
I went with the loops and it worked!
Thanks a lot!
Cheers :D

サインインしてコメントする。

### カテゴリ

Find more on Data Type Conversion in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by