Answer by Paul Panzer for selective row sum matrix in numpy
Probably not the fastest but maybe educational: The operation you are describing can be thought of as matrix multiplication with a certain adjacency matrix:from scipy import sparse# construct adjacency...
View ArticleAnswer by Divakar for selective row sum matrix in numpy
One way would be to sum with indexed on original array and then subtract out the summations caused by the last indexed ones by -1s -out = M[E].sum(1) - M[-1]*(E==-1).sum(1)[:,None]Another way would be...
View ArticleAnswer 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,...
View Articleselective row sum matrix in numpy
Is there any efficient numpy way to do the following:Assume I have some matix M of size R X C. Now assume I have another matrixE which is of shape R X a (where a is just some constant a < C), which...
View Article