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

后端 未结 7 1470
闹比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:19

    I have a similar question here: Writting in sub-ndarray of a ndarray in the most pythonian way. Python 2 .

    Following the solution of previous post for your case the solution looks like:

    columns_to_keep = [1,3] 
    rows_to_keep = [1,3]
    

    An using ix_:

    x[np.ix_(rows_to_keep, columns_to_keep)] 
    

    Which is:

    array([[ 5,  7],
           [13, 15]])
    
    0 讨论(0)
提交回复
热议问题