0

If I have a 2D array of indices:

i = np.array([[0, 0], [1, 1]])

And a 2D array I want to index:

a = np.array([[1, 2], [3, 4]])

How can I index the array to get a 1D array like the following?

np.array([1, 4])
0

2 Answers 2

3

You can use:

a[i.T[0], i.T[1]]

In case you have more dimensions, you can use:

a[tuple(i.T)]
Sign up to request clarification or add additional context in comments.

Comments

0

You just need a simple indexing:

In [11]: a[i[:, 0], i[:, 1]]
Out[11]: array([1, 4])

The first column of i is number of rows and the second one denotes the column number.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.