Numpy - Multiple 3d Array With A 2d Array
I'm trying the following: Given a matrix A (x, y ,3) and another matrix B (3, 3), I would like to return a (x, y, 3) matrix in which the 3rd dimension of A is multiplied by the val
Solution 1:
You can use np.tensordot
-
np.tensordot(A,B,axes=((2),(1)))
Related post to understand tensordot
.
einsum
equivalent would be -
np.einsum('ijk,lk->ijl', A, B)
We can also use A.dot(B.T)
, but that would be looping under the hoods. So, might not be the most preferred one, but it's a compact solution,
Solution 2:
Sorry for the confusion, I think you can do something like this, using simple numpy methods:
First you can reshape A in a way that its fibers (or depth vectors A[:,:,i]) will be placed as columns in matrix C:
C = A.reshape(x*y,3).T
Then using a simple matrix multiplication you can do:
D = numpy.dot(B,C)
Finally bring the result back to the original dimensions:
D.T.reshape([x,y,3])
Post a Comment for "Numpy - Multiple 3d Array With A 2d Array"