1

速度情報を含むベクトルxがあり、インデックスは時間を表します。ここで、サイズを維持して新しいベクトルを作成したいのですが、値は時間間隔の平均に置き換えられます。

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

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

4

2 に答える 2

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
于 2012-03-18T22:41:05.183 に答える
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
于 2012-03-18T22:19:04.613 に答える