问题描述
我以 30 分钟的间隔测量了一些变量.时间戳以 datevec
或 datenum
格式提供.我要计算...
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) ... 每日平均值和
b) ... x
时间的平均值,例如11:30 时的温度,12:00 时的温度等.对我的整个数据集进行平均.
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:根据要求:点击我获取示例数据
/edit 1: As per request: click me for sample data
推荐答案
考虑到 datevec()
输出存储在 tvec
中,数据存储在 x
,用 unique(...,'rows')
分组并用 accumarray()
累加:
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)]
这篇关于MATLAB:在没有循环的情况下平均时间序列数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!