Access Numpy Arary based on other Array

2321 views python

I have a numpy array A of size [2, 10]. I have another array B of length 10, whose values range between 0 and 1. I now want to access A, taking for each column the row specified by B, meaning: The output should have length 10, the i-th entry in the resulting array is A[0, i] if B[i] == 0 else A[1, i]. How do I index that correcly / most elegantly? Thanks.

answered question

np.where(B == 0, A[0], A[1])

2 Answers


I believe this would work:

A[1][i] if B[i] == 0 else A[0][i]

posted this

for i in range(10):

posted this

Have an answer?


Please login first before posting an answer.