Simplification of a line and line output, numpy

I want to extract rows and columns from a matrix using one β€œfancy” fragment, is this possible?

m = matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) 

My goal

 matrix([[1, 3], [7, 9]]) 

Where i have a list of items i want

 d = [0,2] 

I can achieve functionality

 m[d][:,d] 

But is there a simpler expression?

+3
source share
1 answer

You can do this using numpy.ix_ :

 m = matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) d = [0,2] print m[ix_(d,d)] 

which will emit:

 [[1 3] [7 9]] 
+8
source

Source: https://habr.com/ru/post/1403608/


All Articles