I have three matrices W H and V. I want to get keep which stores elementwise multiplication between all column of V and each column of W and do sum by row.
(V has 6 rows, W also has 6 rows. Each column of W (which has 6 elements) multiplies with 6 elements of every column of Vcolumn-by-column. Then sum the results by row)
W = np.random.randint(4,6, size=(6, 4))
H = np.random.randint(1,3, size=(4, 5))
V = np.dot(W,H) + 1
keep = np.array([]).reshape(0,6)
print W
>>[[4 4 5 5]
[4 4 4 4]
[4 5 5 4]
[4 5 5 5]
[5 4 4 5]
[5 4 4 5]]
print V
>>[[28 33 32 37 24]
[25 29 29 33 21]
[28 33 33 37 24]
[30 35 34 39 25]
[28 32 32 37 23]
[28 32 32 37 23]]
# I want the result from only two from four rows of W
group = 2
for k in xrange(group):
# multiply all of each column of V by k-th column of W and sum in row
keep = np.vstack([keep, sum(V[:,:].T*W[:,k])])
print keep, keep.shape
>>[[ 616. 548. 620. 652. 760. 760.]
[ 616. 548. 775. 815. 608. 608.]] (2L, 6L)
I wonder this can be done without for loop? Like sum(V[:,:].T*W[:,0:1] Although, I don't think it is possible because each column of W has to multiplies matrix V in stepwise, I am sure someone has better idea (or confirm it can't).
I try to avoid for loop as this is a part of long algorithm and I hope it can be super fast when group is up to hundreds.