indexes = np.array([[0,1,3],[1,2,4 ]])
data = np.random.rand(2,5)
Now, i would like an array of shape (2,3), where
result[0] = data[0,indexes[0]]
result[1] = data[1,indexes[1]]
What would be the proper way to achieve this? A numpy way that yould generalize to bigger arrays (perhaps even higher dimensional).
Please note the difference to questions like this, where the array of indexes contains tuples. This is not what I am asking.
Edit
A more general formulation of the question would be:
- data.shape == (s0, s1, .., sn)
- indexes.shape == (s0, s1, ..., sn-1, K)
- so, they have all dimension but the last equal
Than
result[i, j, ..., k] = data[i, j,...,k, indexes[i, j, ..., k]]
where
len([i, j, ..., k]) == len(data)-1 == len(indexes) - 1