Reputation: 132
I have two matrices of the following dimensions:
matrix(W): (7, 15)
matrix(X): (268, 7)
I want to multiply each row in X by the first column of W, and then sum up the result. I then want to do this for all columns in W, so that I will have 15 values at the end. The 15 values will be the sum of each of the 268 rows multiplied by a particular column in W.
To be clear:
z1 = (X1 * Wcol1) + (X2 * Wcol1) + .... + (X268 * Wcol1)
z2 = (X1 * Wcol2) + (X2 * Wcol2) + .... + (X268 * Wcol2)
...
z15 = (X1 * Wcol15) + (X2 * Wcol15) + .... + (X268 * Wcol15)
I am currently working with the following:
samples=268
for j in range(samples):
zs = np.array([])
z = X[j,:] * w[:,0]
zs = np.append(zs, z)
print(zs)
I believe this returns the 268 components of the first "z1" (which then need to be summed). I am having trouble generalising this, to do it for all the 15 columns.
Upvotes: 0
Views: 1095
Reputation: 71
It sounds like you would like to calculate the sum of the columns of the matrix product X*W and store the values in an array. Assuming X and W are of class matrix, you can achieve this directly as follows:
Z=np.array(sum(X*W))
Upvotes: 2
Reputation: 1195
import numpy as np
Then take input for the matrices X and W
xmultw = np.matmul(X,W)
res = np.sum(xmultw,axis=0)
Hope this solves your problem
Upvotes: 1