Say I have this array of data
data = np.asarray([[1, 2, 3, 4], ['a', 'b', 'c', 'd'], ['A', 'B', 'C', 'D'], ['x', 'y', 'z', 'zz']])
and these indices, each "pair" corresponding to one cell in the data matrix
indices = np.asarray([[0, 0], [3, 0], [2, 2], [3, 2]])
Now I want to retrieve the data from the specified cells. I can do this via:
searched_data = []
for x, y in coords:
searched_data.append(data[y][x])
Is there a more pythonic or more numpy-ish variation where I can do this in one line by fancy array indexing or something?
I tried (inspired by this post):
x_indexed1 = data[indices[:, 1]][:,[indices[:, 0]]]
but this gives me
[[['1' '4' '3' '4']]
[['1' '4' '3' '4']]
[['A' 'D' 'C' 'D']]
[['A' 'D' 'C' 'D']]]
and this
x_indexed = data[np.ix_(indices[:, 1],indices[:, 0])]
which gives
[['1' '4' '3' '4']
['1' '4' '3' '4']
['A' 'D' 'C' 'D']
['A' 'D' 'C' 'D']]