How can I (efficiently) compute a moving average of a vector?

前端 未结 3 874
忘了有多久
忘了有多久 2020-12-15 00:44

I\'ve got a vector and I want to calculate the moving average of it (using a window of width 5).

For instance, if the vector in question is [1,2,3,4,5,6,7,8]

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-15 01:21

    If you want to preserve the size of your input vector, I suggest using filter

    >> x = 1:8;
    >> y = filter(ones(1,5), 1, x)
    
    y =
         1     3     6    10    15    20    25     30
    
    >> y = (5:end)
    
    y =
         15    20    25    30
    

提交回复
热议问题