1

If I have two numpy arrays like:

a = np.array([[ 0,  1,  2,  3],
              [ 4,  5,  6,  7],
              [ 8,  9, 10, 11]])
b = np.array([ 0, 4, 8])

and I would like to get the index of the column of a that corresponds to the values of b. Here it would be 0.

With something like:

np.where(np.hsplit(a, 4) == b)

I'm able to find the solution but I think it should be some more intuitive way of doing so.

2 Answers 2

0

Take a look at this answer. The only difference is that you need to transpose a.

>>> a = np.array([[ 0,  1,  2,  3],
              [ 4,  5,  6,  7],
              [ 8,  9, 10, 11]])
>>> b = np.array([ 0, 4, 8])
>>> np.all(a.T==b,axis=1)
array([ True, False, False, False])
>>> np.where(np.all(a.T==b,axis=1))[0][0]
0
Sign up to request clarification or add additional context in comments.

Comments

0

I don't know if it is more or less intuitive, but you can transpose a and compare:

np.where( (a.transpose() == b  ).all(axis=1))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.