title: Adding a new row in a matrix, every other row, whose elements are averages of the elements above and below it
body: <p>I have a matrix, say:</p>
  
  <pre><code>A = [1, 2, 3]
      [4, 5, 6]
      [7, 8, 9]
  </code></pre>
  
  <p>and I want to construct another matrix, having two extra rows as such</p>
  
  <pre><code>B = [1,    2,    3]
      [2.5, 3.5, 4.5]
      [4,     5,   6]
      [5.5, 6.5, 7.5]
      [7,     8,   9]
  </code></pre>
  
  <p>where every element on the second and fourth row is the average of the elements above and below it. </p>
  
  <p>I want to do this, in python, numpy, for an n x m matrix. </p>
  
  <p>EDIT: The generalization to this would be to insert the means of two neighboring rows every n rows. For example</p>
  
  <pre><code>     A = [ 1,  2,  3]
           [ 4,  5,  6]
           [ 7,  8,  9]
           [10, 11, 12]
           [13, 14, 15]
           [16, 17, 18]
           [19, 20, 21]
  </code></pre>
  
  <p>and , for n = 3 , B would become</p>
  
  <pre><code>     B = [ 1,  2,  3]
           [ 4,  5,  6]
           [ 7,  8,  9]
           [8.5, 9.5, 10.5]
           [10, 11, 12]
           [13, 14, 15]
           [16, 17, 18]
           [17.5, 18.5, 19.5]
           [19, 20, 21]
  </code></pre>
  
  <p>and so on. The AVERAGE layers are still the average of the one above and the one below them. I am looking for a function that would give this for any n. </p>