I want to piecewise-average a vector in Matlab. Vector x looks like this:
x = 1:15;
Respectively:
x = [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15]
I want to find the mean value over n = 5 elements; therefore, the result-vector y should look like:
y = [1 1.5 2.5 3 4 5 6 7 8 9 10 11 12 13]
The code for generating the vector y should somehow work like this:
y = [
mean ([1])
mean ([1,2])
mean ([1,2,3])
mean ([1,2,3,4])
mean ([1,2,3,4,5])
mean ([2,3,4,5,6])
mean ([3,4,5,6,7])
mean ([4,5,6,7,8])
mean ([5,6,7,8,9])
mean ([6,7,8,9,10])
mean ([7,8,9,10,11])
mean ([8,9,10,11,12])
mean ([9,10,11,12,13])
mean ([10,11,12,13,14])
mean ([11,12,13,14,15])
]
For n < 5 elements, the program should average over n elements. For example, if there are only 3 elements available, the code should average the first 3 elements. For n > 5 elements, the program should average over the last 5 elements.
Any help is appreciated!