3-D Matrix Multiplication in Numpy

825 views python
0

I have to multiply two 2-D matrices, bob and tim, in Numpy Python 3.x

bob.shape gives (2,4)

tim.shape gives (7,4)

This piece of code gives a 3-D matrix with a shape of (2,7,4)

np.array([foo*tim for foo in bob])

It gives the output I want. But, I was wondering if there was a more elegant/faster way to do this in numpy rather than me having to iterate through bob

answered question

1 Answer

3

See Python Broadcasting

bob.reshape((2, 1, 4)) * tim.reshape((1, 7, 4))

posted this

Have an answer?

JD

Please login first before posting an answer.

Ads

Categories