0

Suppose A and B are two 4 dimensional numpy arrays with the same dimension.

A = np.random.rand(5,5,2,10)
B = np.random.rand(5,5,2,10)

a, b, c, d = A.shape

dat = []

for k in range(d):
    sum = 0
    for l in range(c):
        sum = sum + np.einsum('ij,ji->', A[:,:,l,k], B[:,:,l,k])
    dat.append(sum)

I was wondering whether I can use the "einsum" to replace the inner for loop, maybe even outer for loop, or maybe some matrix manipulation to replace all of it, casue the data set is large.

Is there any faster way to achieve this?

Nicolas H
  • 535
  • 3
  • 13

0 Answers0