2012-03-18 6 views
1

나는 속도 정보를 포함하는 벡터 x을 가지고 색인은 시간을 나타냅니다. 내가 4로 시간 간격을 원하는 경우Matlab : 시간 간격이있는 평균?

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

, 출력과 같아야합니다 : 지금은 그 크기를 유지, 새로운 벡터를 만들 좋겠지 만, 값은 시간 간격 등의 평균으로 대체됩니다

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 

그 기능이 있습니까? thanks

+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

초기 입력 벡터의 길이를 보존하면서 입력 데이터 세트에서 스테핑 평균을 수행하는 것으로 보입니다. 내 지식으로는이 일을하는 단일 기능이 없습니다.

그러나 파이썬에서는 쉽게 할 수 있습니다. 예를 들어 :

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