Is there an easy way to index a numpy multidimensional array along the last dimension using an index array? For example, take an array of ashapes (10, 10, 20). Suppose I have an array of indexes b, forms (10, 10), so that the result is c[i, j] = a[i, j, b[i, j]].
I tried the following example:
a = np.ones((10, 10, 20))
b = np.tile(np.arange(10) + 10, (10, 1))
c = a[b]
However, this does not work, because then it tries to index as a[b[i, j], b[i, j]], which does not match a[i, j, b[i, j]]. Etc. Is there an easy way to do this without resorting to a loop?
tiago source
share