I am having a hard time indexing a two-dimensional array given an indexing array.
Suppose I have:
# Indexing array
idx = np.array([0,2,1,2,1])
# Array to be indexed
my_array = np.array([[0,2,1], [0,5,1], [1,2,1], [5,1,3], [2,6,2]])
And the expected output should be the 0 index of the first entry of my_array, the 2 index of the second entry of my_array and so on, thus:
# Expected output
expected_array = np.array([0,1,2,3,6])
I have actually made it using a for-loop:
# Using for-loop
expected_array = np.array([])
for i in range(len(my_array)):
expected_array = np.append(idx, my_array[i][idx[i]])
But I am wondering if there is a way of possibly doing array-indexing without the use of for-loops? Assume that len(idx)==len(my_array) all the time.