MATLAB: Averaging time-series data without loops?

拟墨画扇 提交于 2019-12-18 08:58:25

问题


I have measured a handful of variables in 30 minute intervals. Time stamps are available in datevec or datenum format. I want to calculate ...

a) ... daily averages and
b) ... average values at time x, e.g. temperature at 11:30, temperature at 12:00, etc. averaged over my whole dataset.

While this is, more or less, easily done with loops, I wonder if there is an easier / more convenient way to work with time-series, since this is a quite basic task after all?

/edit 1: As per request: click me for sample data


回答1:


Considering that datevec() output is stored in tvec and data in x, group with unique(...,'rows') and accumulate with accumarray():

% Group by day
[unDates, ~, subs] = unique(tvec(:,1:3),'rows');

% Accumulate by day
[unDates accumarray(subs, x, [], @mean)]

% Similarly by hour
[unHours, ~, subs] = unique(tvec(:,4:5),'rows');
[unHours accumarray(subs, x, [], @mean)]


来源:https://stackoverflow.com/questions/17032033/matlab-averaging-time-series-data-without-loops

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!