title: Numpy Routine(s) to create a regular grid inside a 2d array
body: <p>I am trying to write a function that would create a regular grid of 5 pixels by 5 pixels inside a 2d array.  I was hoping some combination of <code>numpy.arange</code> and <code>numpy.repeat</code> might do it, but so far I haven't had any luck because <code>numpy.repeat</code> will just repeat along the same row.</p>
  
  <p>Here is an example:</p>
  
  <p>Let's say I want a 5x5 grid inside a 2d array of shape <code>(20, 15)</code>.  It should look like:</p>
  
  <pre><code>array([[ 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2],
         [ 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2],
         [ 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2],
         [ 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2],
         [ 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2],
         [ 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5],
         [ 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5],
         [ 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5],
         [ 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5],
         [ 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5],
         [ 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8],
         [ 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8],
         [ 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8],
         [ 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8],
         [ 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8],
         [ 9, 9, 9, 9, 9,10,10,10,10,10,11,11,11,11,11],
         [ 9, 9, 9, 9, 9,10,10,10,10,10,11,11,11,11,11],
         [ 9, 9, 9, 9, 9,10,10,10,10,10,11,11,11,11,11],
         [ 9, 9, 9, 9, 9,10,10,10,10,10,11,11,11,11,11],
         [ 9, 9, 9, 9, 9,10,10,10,10,10,11,11,11,11,11]])
  </code></pre>
  
  <p>I realize I could simply use a loop and slicing to accomplish this, but I could be applying this to very large arrays and I worry that the performance of that would be too slow or impractical.</p>
  
  <p>Can anyone recommend a method to accomplish this?</p>
  
  <p>Thanks in advance.</p>
  
  <p><strong>UPDATE</strong>:</p>
  
  <p>All the answers provided seem to work well.  Can anyone tell me which will be the most efficient to use for large arrays?  By large array I mean it could be <code>100000 x 100000</code> or more with <code>15 x 15</code> grid cell sizes.</p>