简体   繁体   中英

Multiplication of 2D array and 3D array

I have 3D A matrix 3x3x5 (The third dimension is 5) and 2D B matrix (3x3). I want to multiply A and B to obtain (3x3x5) matrix. Then sum the elements of the resulting 3D matrix to create 2D matrix (3x3). How can I do this?

Simply use the * operator to multiply 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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