2020-11-29

Multiply i-th 2-d matrix in numpy 3d array with i-th column in 2d array

Suppose that I have a 3d array A and a 2d array B. A has dimension (s,m,m) while B has dimension (m,s).

I want to write code for a 2d array C with dimension (m,s) such that C[:,i] = A[i,:,:] @ B[:,i].

Is there a way to do this elegantly without using a for loop in numpy?

One solution I thought of was to reshape B into a 3d array with dimension (m,s,1), multiply A and B via A@B, then reshape the resulting 3d array into a 2d array. This sounds a bit tedious and was wondering if tensordot or einsum can be applied here.

Suggestions appreciated. Thanks!



from Recent Questions - Stack Overflow https://ift.tt/37i6enx
https://ift.tt/eA8V8J

No comments:

Post a Comment