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 Answers2

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))
Christian Sloper
  • 7,440
  • 3
  • 15
  • 28
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
Bakhanov A.
  • 146
  • 1
  • 7