I have an array b which is of the shape E x B x 3. I have another array a which specifies which 3 elements to take our of b.
The following code works (in this example E=2, B=4):
import numpy as np
a = [1, 1, 0, 0]
b = np.array([[[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0], [0.0, 0.0, 3.0]],
[[2.0, 0.0, 0.0], [0.0, 2.0, 0.0], [0.0, 0.0, 2.0], [0.0, 0.0, 3.0]]])
# n_pred = np.transpose(n_pred, axes=[1, 0, 2])
c = []
for i, idx in enumerate(a):
c.append(b[idx, i])
c = np.array(c)
print(c)
My question is, is there a more efficient way to do this? (maybe using some built-in numpy function?