我有一个矩阵数组,我想乘以一个向量(因此矩阵中的第一个数组应该乘以向量中的第一个值,等等)。
import numpy as np
# Three matrices/double arrays
a = np.array([[1,2], [3, 4]])
b = np.array([[2,3], [4, 5]])
c = np.array([[3,4], [5, 6]])
# An array of matrices
d = np.array([a, b, c])
# A vector
e = np.array([1,2,3])
# Multiply every matrix by the corresponding value in the vector
f = [ d[i] * e[i] for i in range(len(e)) ]
# Somewhat to my surpise however, this doesn't work
g = d * e # <-- Doesn't work
# Nor does
h = e * d # <-- Doesn't work
所以列表理解有效,但我怀疑这是否是最有效的做事方式。
我是否忽略了一些非常简单的事情?