简体   繁体   English

二维数组与3D数组相乘

[英]Multiplication of 2D array and 3D array

I have 3D A matrix 3x3x5 (The third dimension is 5) and 2D B matrix (3x3).我有 3D A 矩阵 3x3x5(第三维是 5)和 2D B 矩阵(3x3)。 I want to multiply A and B to obtain (3x3x5) matrix.我想将 A 和 B 相乘以获得 (3x3x5) 矩阵。 Then sum the elements of the resulting 3D matrix to create 2D matrix (3x3).然后将生成的 3D 矩阵的元素相加以创建二维矩阵 (3x3)。 How can I do this?我怎样才能做到这一点?

Simply use the * operator to multiply numpy arrays.只需使用*运算符将 numpy 与 arrays 相乘即可。

import numpy as np
a = np.arange(45).reshape(3, 3, 5)
b = np.arange(9).reshape(3, 3)
c = a * b
print(c) # 3x3x5 array
d = np.sum(c, axis=-1)
print(d)

d should be the answer you are looking for. d应该是您正在寻找的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM