How do I outline this expression using Python numpy?

Given the numeric arrays P (N), D (N, N), I want to calculate the array A (N):

A_i = SUM(P_i - P_j) where j is such that D[i][j] is True A_i = 0 otherwise 

So,

 A_i = N*P_i - (P_m + P_n+...) where D[i][m], D[i][n],... are all True 

I could write above using a double loop and enumerate (), but it is rather slow. Just wondering how I can write this in terms of numpy ufuncs.

+4
source share
1 answer

If I understand the question correctly, here is one way to do this.

 A = P*D.sum(axis=1)-D.dot(P) 
+6
source

Source: https://habr.com/ru/post/1492855/


All Articles