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?
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]]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With