3.18 How to find the moving average of a 1D sequence?

Given some sequence such as \(1,2,3,4,5,6,7\) how to find the moving average for different window sizes?

Mathematica

For window size \(k=2\)

v = {1, 2, 3, 4, 5, 6, 7, 8}; 
f = {1/2, 1/2}; 
ListConvolve[f, v] // N
 

{1.5,2.5,3.5,4.5,5.5,6.5,7.5}
 

For window size \(k=3\)

v = {1, 2, 3, 4, 5, 6, 7, 8}; 
f = Table[1/3, {3}]; 
ListConvolve[f, v] // N
 

{2., 3., 4., 5., 6., 7.}
 

 

Matlab

For a window size \(k=2\)

V=[1 2 3 4 5 6 7 8]; 
f=[1/2 1/2]; 
conv(V,f,'valid')
 

ans = 
 1.5000 2.5000 3.5000 4.5000 5.5000 6.5000 7.5000
 

For window size \(k=3\)

V = [1 2 3 4 5 6 7 8]; 
k = 3; 
f = ones(k,1)/k; 
conv(V,f,'valid')
 

ans = 
 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000