If I have two vectors and a sparse mask with mostly 0's and a few 1's, is there an efficient way to compute the masked dot product of a
and b
? My solution is very inefficient because it computes a bunch of zeros that don't contribute to the result:
a = np.random.normal(size=1000)
b = np.random.normal(size=1000)
m = np.random.choice([0,1], size=1000, p=[0.99, 0.01])
prod = np.sum(a*b*m)
(in my actual problem I have n-dim arrays rather than 1-dim, but the question is the same)