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

后端 未结 7 1542
闹比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:10

    As Sven mentioned, x[[[0],[2]],[1,3]] will give back the 0 and 2 rows that match with the 1 and 3 columns while x[[0,2],[1,3]] will return the values x[0,1] and x[2,3] in an array.

    There is a helpful function for doing the first example I gave, numpy.ix_. You can do the same thing as my first example with x[numpy.ix_([0,2],[1,3])]. This can save you from having to enter in all of those extra brackets.

提交回复
热议问题