Say I have a sorted numpy array:
arr = np.array([0.0, 0.0],
[0.5, 0.0],
[1.0, 0.0],
[0.0, 0.5],
[0.5, 0.5],
[1.0, 0.5],
[0.0, 1.0],
[0.5, 1.0],
[1.0, 1.0])
and suppose I make a non trivial operation on it such that I have a new array which is the same as the old one but in another order:
arr2 = np.array([0.5, 0.0],
[0.0, 0.0],
[0.0, 0.5],
[1.0, 0.0],
[0.5, 0.5],
[1.0, 0.5],
[0.0, 1.0],
[1.0, 1.0],
[0.5, 1.0])
The question is: how do you get the indices of where each element of arr2
are placed in arr
. In other terms, I want a method that takes both arrays and return an array the same length as arr2
but with the index of the element of arr
. For example, the first element of the returned array would be the index of the first element of arr2
in arr
.
where_things_are(arr2, arr)
return : array([1, 0, 3, 2, 4, 5, 6, 8, 7])
Does a function like this already exists in numpy?
EDIT:
I tried:
np.array([np.where((arr == x).all(axis=1)) for x in arr2])
which returns what I want, but my question still holds: is there a more efficient way of doing this using numpy methods?
EDIT2:
It should also work if the length of arr2
is not the same as the length of the original array (like if I removed some elements from it). Thus it is not finding and inverting a permutation but rather finding where elements are located at.