title: Quicker Mean and Standard Deviation Calculation for Large Array
body: <p>I am doing some image processing and one of the things I need to do is calculate the mean and standard deviations of 5x5 squares of pixels and assign those values to an array with the same index as the central pixel of the 5x5 square (the mean values and standard deviations can go to separate 2D arrays). At the moment the way I'm doing this is:</p>
  <pre><code>for i in range(2, num_rows+2, 1):
      for j in range(2, num_cols+2, 1):
          #enter pixel values of 5x5 cluster into a list
          pixel_list = [padded_grey[i-2,j-2],padded_grey[i-2,j-1],padded_grey[i-2,j],padded_grey[i-2,j+1],padded_grey[i-2,j+2],padded_grey[i-1,j-2],padded_grey[i-1,j-1],padded_grey[i-1,j],padded_grey[i-1,j+1],padded_grey[i-1,j+2],padded_grey[i,j-2],padded_grey[i,j-1],padded_grey[i,j+1],padded_grey[i,j+2],padded_grey[i+1,j-2],padded_grey[i+1,j-1],padded_grey[i+1,j],padded_grey[i+1,j+1],padded_grey[i+1,j+2],padded_grey[i+2,j-2],padded_grey[i+2,j-1],padded_grey[i+2,j],padded_grey[i+2,j+1],padded_grey[i+2,j+2],padded_grey[i,j]]
  
          #calculate mean and standard deviation of values in list and enter into index of central pixel
          mean_vals[i-2,j-2] = np.mean(pixel_list)
          stddev_vals[i-2,j-2] = np.std(pixel_list)
  </code></pre>
  <p>The reason why the <code>for</code> loops are shifted over by 2 indices is that the array <code>padded_grey</code> is zero-padded to prevent &quot;Out of Range&quot; errors when I am near the edge of the array. Now of course this takes forever to finish executing since I have to go through an entire 1024x1280 array pixel by pixel.</p>
  <p>Could someone please suggest some ways I could optimise this to run faster? Thank you in advance.</p>