title: How to get all sub matrices of 2D array without numpy?
body: <p>I need to get all submatrices of the 2D array and to do the manipulation for each submatrix. So I created example matrix:</p>
  
  <pre><code>M3 = [list(range(5)) for i in range(6)]
  [[0, 1, 2, 3, 4],
   [0, 1, 2, 3, 4],
   [0, 1, 2, 3, 4],
   [0, 1, 2, 3, 4],
   [0, 1, 2, 3, 4],
   [0, 1, 2, 3, 4]]
  </code></pre>
  
  <p>I need to capture 3 rows and 3 columns and then shift this "window" till I get all submatrices. The first submatrix would be:</p>
  
  <pre><code> [[0, 1, 2],
   [0, 1, 2],
   [0, 1, 2]]
  </code></pre>
  
  <p>and the last one is:</p>
  
  <pre><code> [[2, 3, 4],
   [2, 3, 4],
   [2, 3, 4]]
  </code></pre>
  
  <p>For this matrix I need 12 submatrices. However, I become more using code with which I tried to solve the problem:</p>
  
  <pre><code>for j in range(len(M3[0])-3):
     for i in range(len(M3)-3):
         for row in M3[0+j:3+j]:
             X_i_j = [row[0+i:3+i] for row in M3[0+j:3+j]]
             print(X_i_j)
  </code></pre>
  
  <p>I get 18 but not 12 (with two duplicates of each submatrix):</p>
  
  <pre><code>[[0, 1, 2], [0, 1, 2], [0, 1, 2]]
  [[0, 1, 2], [0, 1, 2], [0, 1, 2]]
  [[0, 1, 2], [0, 1, 2], [0, 1, 2]]
  [[1, 2, 3], [1, 2, 3], [1, 2, 3]]
  [[1, 2, 3], [1, 2, 3], [1, 2, 3]]
  [[1, 2, 3], [1, 2, 3], [1, 2, 3]]
  ...
  [[2, 3, 4], [2, 3, 4], [2, 3, 4]]
  [[2, 3, 4], [2, 3, 4], [2, 3, 4]]
  </code></pre>
  
  <p>And with this sample of code I get 6 submatrices with 1 duplicate for each:</p>
  
  <pre><code>for i in range(len(M3)-3):
     for j in range(len(M3[0])-3):
         X_i_j = [row[0+i:3+i] for row in M3[0+j:3+j]]
         print(X_i_j)
  </code></pre>
  
  <p>I do not see what is wrong with it and why I get the duplicates. How can I get all sub matrices of 2D array without numpy for this case?</p>