Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simplfy row AND column extraction, numpy [duplicate]

Tags:

python

numpy

I wish to extract rows and columns from a matrix using a single "fancy" slice, is this possible?

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

My target is

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

Where I have a list of the items I want

d = [0,2]

I can achieve the functionality by

m[d][:,d]

But is there a simpler expression?

like image 339
poulter7 Avatar asked Apr 03 '26 03:04

poulter7


1 Answers

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]]
like image 122
talonmies Avatar answered Apr 04 '26 17:04

talonmies



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!