# Element wise multiplication of the last dimension

Hello everyone,

I have the following two tensors:

`E` with size `(3, 2, 4)`, lets visualize it as
`e11, e12`
`e21, e22`
`e31, e32`
where every `e` is a vector with dimension 4

`G` with size `(3, 2)`, lets visualize it as
`g11, g12`
`g21, g22`
`g31, g32`
where every `g` is a scalar

As a result I want to multiply every vector e of E with the corresponding scalar g from G as following:
`g11*e11, g12*e12`
`g21*e21, g22*e22`
`g31*e31, g32*e32`
where * is element wise multiplication, or in other words every vector e from the tensor `E` (let’s say `e11`), I want it multiplied with the corresponding scalar (or `g11` for `e11`), etc… The size of the result should be `(2, 2, 4)`, same as the `E` tensor.

Thank you.

1 Like

I found out that first unsqueezing the G tensor, repeating it 4 times along the 3-th dimension, and element-wise multiplying it with E does the job, but there may be a more elegant solution. Here is the code:

`G_tmp = G.unsqueeze(2).expand(-1, -1, 4)`
`res = G_tmp * E`

Feel free to correct me, or propose a more elegant solution You don’t need expand() there, tensor broadcasting takes care of it.