Quantcast
Viewing all articles
Browse latest Browse all 4

Answer by yatu for selective row sum matrix in numpy

You could index M with E, and np.sum where the actual indices in E are greater or equal to 0. For that we have the where parameter:

np.sum(M[E], where=(E>=0)[...,None], axis=1)array([[5, 7, 9],       [7, 8, 9],       [1, 2, 3]])

Where we have that:

M[E]array([[[1, 2, 3],        [4, 5, 6]],       [[7, 8, 9],        [7, 8, 9]],       [[7, 8, 9],        [1, 2, 3]]])

Is added on the rows:

(E>=0)[...,None]array([[[ True],        [ True]],       [[ True],        [False]],       [[False],        [ True]]])

Viewing all articles
Browse latest Browse all 4

Trending Articles