RookieScientist
RookieScientist

Reputation: 313

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?

Upvotes: 1

Views: 293

Answers (1)

EnderShadow8
EnderShadow8

Reputation: 1050

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.

Upvotes: 1

Related Questions