Skip to content Skip to sidebar Skip to footer

Index Column Values In 2d Array Using Array Of Indices

I have the following array: import numpy as np print(A) array([[ 0, 1, 4, 5, 8, 7], [ 5, 3, 4, 1, 8, 11], [ 2, 7, 5, 3, 4, 1], [ 2, 8, 8, 1,

Solution 1:

You can use advanced indexing. You need to define an indexing array across the first axis, so that both indexing arrays are broadcast together and each column index refers to a specific row. In this case you just want an np.arange to index on the rows:

A[np.arange(A.shape[0]), b]
# array([ 7,  5,  3, 10,  5])

Post a Comment for "Index Column Values In 2d Array Using Array Of Indices"