1

I have a big array of allowed integer values, and I want to figure out the entries' value by finding their corresponding index in a different array containing all possible values.

It is the same problem that can be solved with np.where() with individual numbers:

E.g.

my_val = 3
possible_val_array = np.array([-1, 2, 3])
idx = np.where(my_val==possible_val_array)[0][0]

This returns:

idx
2

Now I want to do this with a whole array of values all at once. I know this can be done with np.searchsorted() and some other functions when my arrays are 1D. However, I would like to do this with a multidimensional array:

E.g.

my_val_array = np.array([(-1,2,3),(3,-1,-1)])
possible_val_array = np.array([-1, 2, 3])
idx = np.where(my_val_array==possible_val_array)

This would return:

idx  
array([[0, 1, 2],
       [2, 0, 0]])

Is there any way to do this without reshaping my array to be 1D?

Slash
  • 285
  • 2
  • 13

0 Answers0