I have a number of operations in numpy which I can perfectly perform in a loop, but I haven't been able to vectorise them in one numpy call.
# data matrix
d = np.random.rand(1496, 1, 2)
# boolean matrix
r = np.random.rand(5, 1496, 1, 2) > 0.5
# result matrix
x = np.empty((5,))
# How can I avoid this loop?
for i in xrange(r.shape[0]):
x[i] = d[r[i]].sum()
Is it possible to speed things up by somehow vectorising the loop?