Numpy - Dot Product of a Vector of Matrices with a Vector of Scalars
If your NumPy is new enough (1.6 or better), you could use numpy.einsum:
result = np.einsum('ijk,i -> jk', data, vector)
In [36]: data = np.array ([[[1,1,1,1],[2,2,2,2],[3,3,3,3]], [[3,3,3,3],[4,4,4,4],[5,5,5,5]]])
In [37]: vector = np.array ([10,20])
In [38]: np.einsum('ijk,i -> jk', data, vector)
Out[38]:
array([[ 70, 70, 70, 70],
[100, 100, 100, 100],
[130, 130, 130, 130]])
Or, without np.einsum
, you could add extra axes to vector
and take advantage of broadcasting to perform the multiplication:
In [64]: (data * vector[:,None,None]).sum(axis=0)
Out[64]:
array([[ 70, 70, 70, 70],
[100, 100, 100, 100],
[130, 130, 130, 130]])