简体   繁体   English

numpy 元素乘向量和矩阵具有相同的行数

[英]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.

相关问题 numpy:矩阵乘法比矢量和快吗? - numpy: is matrix multiplication faster than sum of a vector? 混淆矩阵错误:数据和参考因素必须具有相同的水平数 - Error in Confusion Matrix : the data and reference factors must have the same number of levels 如何在不使用numpy或tensorflow中的任何循环的情况下获取矩阵行? - How to get rows of matrix without using any loops in numpy or tensorflow? CUDA内存不足,矩阵乘法 - CUDA out of memory with matrix multiply 为什么混淆矩阵中的总数与输入的数据不同? - Why the the total number in confusion matrix not same as the data input? 如何在 Pandas 数据框中仅保存具有特定 numpy 数组/矩阵形状的行? - How to save only the rows with a specific numpy array / matrix shape in Pandas dataframe? Keras Lambda层:将张量与常数矩阵相乘 - Keras Lambda layer: multiply a tensor with a matrix of constant numpy数组的多维矩阵乘法 - Multidimensional matrix multiplication on numpy array 如何在 Python 中缩放 numpy 矩阵? - How to scale numpy matrix in Python? pytorch DataLoader:`张量必须具有相同的维数` - pytorch DataLoader: `Tensors must have same number of dimensions`
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM