title: Speed up a loop with values replacement
body: <p>I have a 2D matrix. I want to compare each value with the mean of the values in a box around it(excluding the value itself). If the value is bigger than the mean plus 3sigma it will be replaced by the mean of the values in the box. Then I repeat the process over the matrix five times.
  So, I have these pice of code that works just fine in my little example matrix, but when I go to the real one (750X2000) it takes forever to run. Is there any way to speed up this loop? Im relatively new to python, so maybe there are some basic, obvious things that Im not seeing.</p>
  <pre><code>    import numpy as np
      mat= [[ 0 , 2 , 4 , 60000 , 8],[10 ,12, 14000, 16, 18],[20 ,22, 625, 26, 28],[30, 32,34, 36, 38],[10000, 42, 44, 46, 48]] 
      mat=np.array(mat)
      print(mat)
      print(mat.shape[0])
      print('###############')
      b=0
      while b&lt;5:
            for i in range(mat.shape[0]):
                 for j in range(mat.shape[1]):
                     chunk = mat[max(0,i-1):i+2, max(0,j-1):j+2]
                     c = np.setdiff1d(chunk,mat[i,j])
                     media_c=np.mean(c)
                     #print(media_c)
                     sdv=np.std(c)
                     if mat[i,j]&gt;media_c+3*sdv:
                         mat[i,j]=media_c
          
            b+=1
      
       print(mat)
  </code></pre>