Slicing of a NumPy 2d array, or how do I extract an mxm submatrix from an nxn array (n>m)?

后端 未结 7 1539
闹比i
闹比i 2020-11-27 09:30

I want to slice a NumPy nxn array. I want to extract an arbitrary selection of m rows and columns of that array (i.e. without any pattern in the numbers of rows/col

7条回答
  •  离开以前
    2020-11-27 10:17

    I'm not sure how efficient this is but you can use range() to slice in both axis

     x=np.arange(16).reshape((4,4))
     x[range(1,3), :][:,range(1,3)] 
    

提交回复
热议问题