0

I have two arrays:

L, M, N = 6, 31, 500
A = np.random.random((L, M, N))
B = np.random.random((L, L))

I am trying to get an array C such that:

C = B * A

C has dimension  [L, M, N]  

I tried answer posted at this link but it hasn't given me the desired output.

A for loop version of above code is:

L, M, N = 6, 31, 500
A = np.random.random((L, M, N))
B = np.random.random((L, L))

z1 = []
for j in range(M):
    a = np.squeeze(A[:, j, :])
    z1.append(np.dot(B, a))

z2 = np.stack(z1)
2
  • If you make the 2 dimensions of B different, there'll be less ambiguity. I don't think you need the squeeze. A[:,j:] will be 2d. Commented Aug 6, 2018 at 22:11
  • 1
    np.einsum('kl,lmn->kmn', B, A) should work; but your iterative solution implies a 'mkn' order. Commented Aug 6, 2018 at 22:13

1 Answer 1

1

I think you are looking for numpy.tensordot() where you can specify along which axes to sum:

np.tensordot(B,A,axes=(1,0))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.