Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
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])
You can use:
a[i.T[0], i.T[1]]
In case you have more dimensions, you can use:
a[tuple(i.T)]
Add a comment
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.
i
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.