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

查看:1917
本文介绍了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分钟间隔的平均值/SD(采用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天全站免登陆