Let's assume we have a bunch of 3 by 3 matrices along with a bunch of 3-dimensional vectors:
N = 100
matrices = np.random.rand(N, 3, 3) # shape: (100, 3, 3)
vectors = np.random.rand(N, 3) # shape: (100, 3)
How can I perform an "element-wise" matrix/vector multiplication, so that e.g. result[0]
is the 3-dimensional vector resulting from the matrix/vector multiplication of matrices[0]
with vector[0]
.
Using np.dot
, np.matmul
, or np.prod
directly fails due to the non-matching shapes. Is there a broadcasting trick to make this work?