Reputation: 13
Say x
is a 3x3 numpy array that contains the following:
import numpy as np
x = np.array([[ 1., 2., 3.],
[ 4., 5., 6.],
[ 7., 8., 9.]])
is there some indexing that can give me the following subarray:
array([[ 1., 2.],
[ 5., 6.]])
Upvotes: 1
Views: 258
Reputation: 152870
You can use integer array indexing with a tuple of arrays:
>>> rows = np.array([[0, 0],
... [1, 1]], dtype=np.intp)
>>> columns = np.array([[0, 1],
... [1, 2]], dtype=np.intp)
>>> x[rows, columns]
array([[ 1., 2.],
[ 5., 6.]])
Upvotes: 4