calculate statistics every quarter of an hour on time series data

1 次查看(过去 30 天)
I have time series data in a table labeled with the date and time. I want to calculate statistics on every quarter of an hour. The number of observations every quarter of an hour can vary. The code I have takes a long time as the time series can be large.
D = datevec(x.DateTime,'yyyy-mm-dd HH:MM:SS');
quarter = zeros(N,1);
quarter(D(:,5)>00 & D(:,5)<=15) = 1;
quarter(D(:,5)>15 & D(:,5)<=30) = 2;
quarter(D(:,5)>30 & D(:,5)<=45) = 3;
quarter(D(:,5)>45 & D(:,5)<=60) = 4;
quarter(D(:,5)==00) = 4;
qnow = quarter(1);
while ~isempty(quarter)
stop = find(quarter~=qnow,1)-1;
x_temp = x(1:stop,:);
x(1:stop,:) = [];
quarter(1:stop) = [];
if qnow < 4
qnow = qnow + 1;
else
qnow = 1;
end
length(quarter)
end
At each iteration I can calculate statistics on x_temp and save to another table. Is there a more efficient way to do this?
Thanks,

回答(1 个)

Azzi Abdelmalek
Azzi Abdelmalek 2015-6-27
x=datenum('2015-01-01 00:00:0'):1/(24*12):datenum('2015-01-01 2:00:0')
y=datestr(x,'yyyy-mm-dd HH:MM:SS')
D = datevec(y,'yyyy-mm-dd HH:MM:SS')
time=D(:,5);
idx1=time>=00 & time<=15
idx2=time>15 & time<=30
idx3=time>30 & time<=45
idx4=time>45 & time<=60
v={idx1 idx2 idx3 idx4}
ii1=sort(cell2mat(cellfun(@(x) strfind([0 x' 0],[0 1]),v,'un',0)))
ii2=sort(cell2mat(cellfun(@(x) strfind([0 x' 0],[1 0])-1,v,'un',0)))
You can use all the indices of different groups in ii1 and ii2.
  1 个评论
stephen
stephen 2015-6-27
That works great. Is there a better way than a for loop to then apply a function like mean to the data given the indices? So I want to take the mean of each of groups given by ii1 and ii2.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Dates and Time 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by