What is the quickest way to multiply a matrix against a numpy array of vectors? I need to multiply a matrix A by every single vector in a list of 1000 vectors. Using a for loop is taking too long, so I was wondering if there's a way to multiply them all at once?
Example:
arr = [[1,1,1], [1,1,1],[1,1,1]]
A=
[2 2 2]
[2 2 2]
So I need to multiply Av for each v in arr. The result:
arr = [[6,6], [6,6], [6,6]]
Is there a faster way than:
new_arr = []
for v in arr:
sol = np.matmul(A, v)
new_arr.append(sol)
A. Is the other thing a list or array? If array what's the dtype? What's the shape of the 'vectors'?matmul(anddot) is last dimension ofApairs with the 2nd to the last ofB(or the only one ofv). Have you triedmatmul(A, arr.T)?Ais (2,3) andarris (4,3), it should be clearer that you want to pair the 3's, and get a (2,4) or (4,2) result. To get that a transpose of eitherA` orarris required.