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