2012-03-18 12 views
1

私は速度情報を含むベクトルxを持っていて、インデックスは時間を表しています。今、私はそのサイズを維持し、新しいベクトルを作成したいが、値は、時間間隔などの平均値に置き換えられます:Matlab:平均時間間隔ですか?

x = 
    101 
    102 
    103 
    104 
    105 
    106 
    107 
    108 
    109 
    110 
    111 
    112 

私は4であることを時間間隔にしたい場合は、出力は次のようになります。

o = 
102.5 
102.5 
102.5 
102.5 
106.5 
106.5 
106.5 
106.5 
110.5 
110.5 
110.5 
110.5 

これを行う機能はありますか?ありがとう

+0

これは、移動平均が必要なように読み取ります。あれは正しいですか? – PengOne

+0

私はあなたが達成しようとしていることを完全に理解していないと思うが、あなたは[smooth](http://www.mathworks.com/help/toolbox/curvefit/smooth.html) 。 –

+0

はい、移動平均は私が探していたものです!それを行う組み込み関数はありますか?私は滑らかにしました、それは非常に滑らかではありません、私のグラフがあまりにも 'ノイズが多い'ため、平均を見つけることがノイズを減らすことができると思います。 – Bonk

答えて

1

ここにあなたの時間ベクトルは、いくつかの巧妙なインデックスでaccumarrayを組み合わせたインターバル長の正確な倍数であることを必要としない方法があります。

x = [101 
    102 
    103 
    104 
    105 
    106 
    107 
    108 
    109 
    110 
    111 
    112]; 

intervalLength = 4; 

%# create index array 
%# for array of length 10, 
%# intervalLength 4, this gives 
%# [1 1 1 1 2 2 2 2 3 3]' 
idx = zeros(length(x),1); 
idx(1:intervalLength:end) = 1; 
idx = cumsum(idx); 

%# average time 
avg = accumarray(idx,x,[],@mean); 

%# create output array - use index to replicate values 
out = avg(idx); 

out = 
    102.5 
    102.5 
    102.5 
    102.5 
    106.5 
    106.5 
    106.5 
    106.5 
    110.5 
    110.5 
    110.5 
    110.5 
0

初期入力ベクトルの長さを維持しながら、入力データセット全体でステッピング平均を実行しようとしているようです。私の知る限り、これを行う単一の機能はありません。

しかし、あなたはかなり簡単にPythonでそれを行うことができます。たとえば:

def blurryAverage(inputCollection, step=1): 
    """ Perform a tiling average of an input data set according to its 
    step length, preserving the length of the initial input vector """ 

    # Preconditions 
    if (len(inputCollection) % step != 0): 
     raise ValueError('Input data must be of divisible length') 

    ret = [] 
    for i in range(len(inputCollection)/step): 
     tot = 0.0 
     for j in range(step): 
      tot += inputCollection[(i*step)+j] 

     for j in range(step): 
      ret.append(tot/step) # Implicit float coercion of step 

    return ret 


>>> blurryAverage([1,2,3,4,5,6],3) 
[2.0, 2.0, 2.0, 5.0, 5.0, 5.0] 

>>> blurryAverage([1,2,3],4) 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "<stdin>", line 3, in blurryAverage 
ValueError: Input data must be of divisible length 
関連する問題