Creating daily average of data set from half hourly data

조회 수: 5 (최근 30일)
Sophie Stringer
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
Adam Danz 2019년 4월 26일
편집: 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
Sophie Stringer 2019년 4월 27일
I went with the loops and it worked!
Thanks a lot!
Cheers :D

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by