MATLAB:计算时间序列的每个 1 分钟间隔的平均值 [英] MATLAB: compute mean of each 1-minute interval of a time-series

查看:60
本文介绍了MATLAB:计算时间序列的每个 1 分钟间隔的平均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆时间序列,每个时间序列由两个组件描述,一个时间戳向量(以秒为单位)和一个测量值向量.时间向量是不均匀的(即以不规则的间隔采样)

I have a bunch of times-series each described by two components, a timestamp vector (in seconds), and a vector of values measured. The time vector is non-uniform (i.e. sampled at non-regular intervals)

我正在尝试计算每个 1 分钟间隔值的平均值/标准差(取 X 分钟间隔,计算其平均值,取下一个间隔,...).

I am trying to compute the mean/SD of each 1-minutes interval of values (take X minute interval, compute its mean, take the next interval, ...).

我当前的实现使用循环.这是我到目前为止的一个示例:

My current implementation uses loops. This is a sample of what I have so far:

t = (100:999)' + rand(900,1);       %' non-uniform time
x = 5*rand(900,1) + 10;             % x(i) is the value at time t(i)

interval = 1;         % 1-min interval
tt = ( floor(t(1)):interval*60:ceil(t(end)) )';  %' stopping points of each interval
N = length(tt)-1;

mu = zeros(N,1);
sd = zeros(N,1);

for i=1:N
    indices = ( tt(i) <= t & t < tt(i+1) ); % find t between tt(i) and tt(i+1)
    mu(i) = mean( x(indices) );
    sd(i) = std( x(indices) );
end

我想知道是否有更快的矢量化解决方案.这很重要,因为我有大量的时间序列要处理,每个比上面显示的示例要长得多..

I am wondering if there a faster vectorized solution. This is important because I have a large number of time-series to process each much longer than the sample shown above..

欢迎任何帮助.

感谢大家的反馈.

我将生成 t 的方式更正为始终单调递增(排序),这并不是真正的问题..

I corrected the way t is generated to be always monotonically increasing (sorted), this was not really an issue..

另外,我可能没有清楚地说明这一点,但我的目的是在几分钟内找到任何间隔长度的解决方案(1 分钟只是一个例子)

Also, I may not have stated this clearly but my intention was to have a solution for any interval length in minutes (1-min was just an example)

推荐答案

唯一合乎逻辑的解决方案似乎是......

The only logical solution seems to be...

好的.我觉得有趣的是,对我来说只有一个合乎逻辑的解决方案,但许多其他人找到了其他解决方案.无论如何,解决方案似乎很简单.给定向量 x 和 t,以及一组等距断点 tt,

Ok. I find it funny that to me there is only one logical solution, but many others find other solutions. Regardless, the solution does seem simple. Given the vectors x and t, and a set of equally spaced break points tt,

t = sort((100:999)' + 3*rand(900,1));     % non-uniform time
x = 5*rand(900,1) + 10;             % x(i) is the value at time t(i)

tt = ( floor(t(1)):1*60:ceil(t(end)) )';

(请注意,我在上面对 t 进行了排序.)

(Note that I sorted t above.)

我会在三个完全矢量化的代码行中完成此操作.首先,如果间隔是任意的并且间距可能不相等,我会使用 histc 来确定数据系列属于哪个间隔.鉴于它们是统一的,只需执行以下操作:

I would do this in three fully vectorized lines of code. First, if the breaks were arbitrary and potentially unequal in spacing, I would use histc to determine which intervals the data series falls in. Given they are uniform, just do this:

int = 1 + floor((t - t(1))/60);

同样,如果不知道 t 的元素已排序,我会使用 min(t) 而不是 t(1).完成后,使用 accumarray 将结果减少为均值和标准差.

Again, if the elements of t were not known to be sorted, I would have used min(t) instead of t(1). Having done that, use accumarray to reduce the results into a mean and standard deviation.

mu = accumarray(int,x,[],@mean);
sd = accumarray(int,x,[],@std);

这篇关于MATLAB:计算时间序列的每个 1 分钟间隔的平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆