Home > Back-end >  Taking a 3*3 subset matrix from from a really large numpy ndarray in Python
Taking a 3*3 subset matrix from from a really large numpy ndarray in Python

Time:09-28

I am trying to take a 3*3 subset from a really large 400 x 500 ndarray of numpy. But due to some reason, I am not getting the desired result. Rather it is taking the first three rows as a whole.

Here is the code that I wrote.

subset_matrix = mat[0:3][0:3]

But this is what I am getting in my output of my Jupyter Notebook

array([[91, 88, 87, ..., 66, 75, 82],
       [91, 89, 88, ..., 68, 78, 84],
       [91, 89, 89, ..., 72, 80, 87]], dtype=uint8)

CodePudding user response:

mat[0:3][0:3] slice the axis 0 of the 2D array twice and is equivalent to mat[0:3]. What you need is mat[0:3,0:3].

  • Related