Issue
Assume we have a 4*4 matrix F in a numpy array with shape (4, 4)
F=np.array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15]])
and we want to scale F by every elements in matrix A of shape (3,2)
A=np.array([[0, 1],
[2, 3],
[4, 5]])
so the output is a np.array of shape (3, 2, 4, 4) (containing all the 6 scaled matrices of F) like this:
output=array([[[[ 0, 0, 0, 0],
[ 0, 0, 0, 0],
[ 0, 0, 0, 0],
[ 0, 0, 0, 0]],
[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15]]],
[[[ 0, 2, 4, 6],
[ 8, 10, 12, 14],
[16, 18, 20, 22],
[24, 26, 28, 30]],
[[ 0, 3, 6, 9],
[12, 15, 18, 21],
[24, 27, 30, 33],
[36, 39, 42, 45]]],
[[[ 0, 4, 8, 12],
[16, 20, 24, 28],
[32, 36, 40, 44],
[48, 52, 56, 60]],
[[ 0, 5, 10, 15],
[20, 25, 30, 35],
[40, 45, 50, 55],
[60, 65, 70, 75]]]])
Solution
output = np.einsum('ij,kl->ijkl', A, F)
or
output = F * A.reshape((3,2,1,1))
Basically, all you need to do is reshape A in a way that each number is in its own nested list. i.e. [[[[[0]],[[1]]],[[[2]],[[3]]],[[[4]],[[5]]]
.
Answered By - dpkass
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.