# NumPy 中的按元素矩阵乘法

## 在 Python 中使用 `np.multiply()` 方法进行矩阵的元素乘法

Python `NumPy` 库的 `np.multiply(x1, x2)` 方法将两个矩阵 `x1``x2` 作为输入，对输入执行逐元素相乘，然后返回所得矩阵作为输入。

``````import numpy as np

a1 = np.array([[12,46,23,7,2],[3,5,8,3,6]])
a2 = np.array([[15,26,2,17,22],[13,8,9,3,4]])

print(np.multiply(a1,a2))
``````

``````[[ 180 1196   46  119   44]
[  39   40   72    9   24]]
``````

``````import numpy as np

a1 = np.array([[12,46,23,7,2],[3,5,8,3,6]])
a2 = np.array([[15,26,2,17,22],[13,8,9,3,4]])

print(np.multiply(a1[0,:],a2[1,:]))
print(np.multiply(a1[1,:],a2[0,:]))
print(np.multiply(a1[:,3],a2[:,1]))
``````

``````[156 368 207  21   8]
[ 45 130  16  51 132]
[182  24]
``````

## 在 Python 中使用`*`运算符对矩阵进行元素明智的乘法

``````a1 = np.array([[12,46,23,7,2],[3,5,8,3,6]])
a2 = np.array([[15,26,2,17,22],[13,8,9,3,4]])

print(a1*a2)
``````

``````[[ 180 1196   46  119   44]
[  39   40   72    9   24]]
``````

``````import numpy as np

a1 = np.array([[12,46,23,7,2],[3,5,8,3,6]])
a2 = np.array([[15,26,2,17,22],[13,8,9,3,4]])

print(a1[0,:]*a2[1,:])
print(a1[1,:]*a2[0,:])
print(a1[:,3]*a2[:,1])
``````

``````[156 368 207  21   8]
[ 45 130  16  51 132]
[182  24]
``````

• NumPy 归一化矩阵
• NumPy 矩阵减法
• NumPy 矩阵向量乘法
• NumPy 矩阵索引