Arrays 将矩阵列乘以数组元素twise

Arrays 将矩阵列乘以数组元素twise,arrays,numpy,matrix,multiplication,Arrays,Numpy,Matrix,Multiplication,我有一个矩阵: [[1, 2, 3], [0, 1, 0], [4, 5, 6]] 这个阵列: [2, 4, 0.5] 我想将每一行乘以数组elementwise: [[2, 8, 1.5], [0, 4, 0], [8, 20, 3]] 还有,相同,但有列: [[2, 4, 6], [0, 4, 0], [2, 2.5, 3]] 我该怎么办 @约翰 这是我的代码: import numpy as np mx = np.matrix([[0,0,0,0],[1,1,1,1]

我有一个矩阵:

[[1, 2, 3],
 [0, 1, 0],
 [4, 5, 6]]
这个阵列:

[2, 4, 0.5]
我想将每一行乘以数组elementwise:

[[2, 8, 1.5],
 [0, 4, 0],
 [8, 20, 3]]
还有,相同,但有列:

[[2, 4, 6],
 [0, 4, 0],
 [2, 2.5, 3]]
我该怎么办

@约翰

这是我的代码:

import numpy as np

mx = np.matrix([[0,0,0,0],[1,1,1,1],[2,3,4,5],[6,7,8,9]])
print 'matrix:'
print mx
print '\n'

v = np.array([20,0,10,5])
print 'narray'
print v
print '\n'
我无法得到我需要的:

print mx * v

ValueError: shapes (4,4) and (1,4) not aligned: 4 (dim 1) != 1 (dim 0)
以及:

第一点很简单:

a * b
第二种方法是:

a * b[:,np.newaxis]
首先将3矢量
b
转换为3x1矩阵。这相当于:

a * b.reshape(-1, 1)
或:


请注意,b[:,np.nexaxis]等同于b[:,None]。因此,整个操作由
a*b*b[:,None]
给出。
a * b.reshape(-1, 1)
(a.T * b).T