pythonnumpy
Ben Gorman

Ben Gorman

Life's a garden. Dig it.

Calculate the element-wise outer product of two matrices, A & B

import numpy as np
 
A = np.arange(10*3).reshape((10,3))
B = np.arange(10*5).reshape((10,5))
 
print(A)
# [[ 0  1  2]
#  [ 3  4  5]
#  [ 6  7  8]
#  [ 9 10 11]
#  [12 13 14]
#  [15 16 17]
#  [18 19 20]
#  [21 22 23]
#  [24 25 26]
#  [27 28 29]]
 
print(B)
# [[ 0  1  2  3  4]
#  [ 5  6  7  8  9]
#  [10 11 12 13 14]
#  [15 16 17 18 19]
#  [20 21 22 23 24]
#  [25 26 27 28 29]
#  [30 31 32 33 34]
#  [35 36 37 38 39]
#  [40 41 42 43 44]
#  [45 46 47 48 49]]

The result should be a 10x3x5 array where the ith 3x5 array is the outer product of A i A_i Ai and B i B_i Bi.


Solution

This content is gated

Subscribe to one of the products below to gain access