[英]numpy elementwise multiply vector and matrix that have the same number of rows
I have a matrix and a vector that both have n rows.我有一个矩阵和一个向量,它们都有 n 行。 I would like to have the kth row of the matrix be elementwise multiplied by the kth element in my vector, without a for loop.我想让矩阵的第 k 行按元素乘以向量中的第 k 个元素,而不使用 for 循环。 I have already tried np,multiply.我已经尝试过 np,multiply。 and it doesn't work.它不起作用。
Is this possible?这可能吗? Thanks for your help.谢谢你的帮助。
numpy.multiply can do it. numpy.multiply 可以做到。 It has matrix multiply semantics so you need to transpose the matrix.它具有矩阵乘法语义,因此您需要转置矩阵。 Is below what you were looking for?是否低于您要查找的内容?
In [19]: >>> x1 = np.arange(9.0).reshape((3, 3))
In [20]: >>> x2 = np.arange(3.0)
In [21]: np.multiply(x1.T, x2.T).T
Out[21]:
array([[ 0., 0., 0.],
[ 3., 4., 5.],
[12., 14., 16.]])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.