Reputation: 71
Given a N by M array W and a vector V of size N, how do I take the dot product V with every column of W, resulting in a 1-D array D of size M with each element of D consisting out of the dot product of V and W[:,i].
So something like
V = np.random.int(N)
W = np.random.int((N,M))
D = np.zeros(M)
for i in np.arange(M):
D[i] = dotproduct(V,W[:,i])
Is there a way to do this using just numpy arrays and numpy functions? I want to avoid using for loops.
Upvotes: 1
Views: 1043
Reputation: 504
Use np.dot()
v = np.random.randint(3,size = 3)
w =np.random.randint(9, size = (3,3))
np.dot(v,w)
Upvotes: 1
Reputation: 71
Using numpy broadcasting you can simply multiply the vector V and matrix W
V = np.random.randint(N)
W = np.random.randint((N,M))
D = np.sum(V.T*W,axis=0)
Upvotes: 0