If I have a 2D numpy array that I want to extract elements using a list of row,col index pairs.
xy = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
idx = np.array([[0, 0], [1, 1], [2, 2]])
The for loop solution:
elements = list()
for i in idx:
elements.append(xy[idx[i][0], xy[idx[i][1])
Output:
print(elements)
>> [1, 5, 9]
I found the solution if there idx is a list of tuples but I hoping for a solution where there is no need to convert the idx to tuples first.