How can I (efficiently) compute a moving average of a vector?
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] , then the first entry of the resulting vector should be the sum of all entries in [1,2,3,4,5] (i.e. 15 ); the second entry of the resulting vector should be the sum of all entries in [2,3,4,5,6] (i.e. 20 ); etc. In the end, the resulting vector should be [15,20,25,30] . How can I do that? jub0bs The conv function is right up your alley: >> x = 1:8; >> y = conv(x, ones(1,5), 'valid') y = 15 20 25 30 Benchmark Three answers, three different