Multiply vector tensor in matrix tensor

Hi, I have two tensors, a matrix A and a vector B,

A=[[1,2,3],[4,5,6],[7,8,9]]
B=[1,10,100]

I want to multiply 1*[1,2,3], 10*[4,5,6], and 100*[7,8,9], and get the result as:
C=[[1,2,3],[40,50,60],[700,800,900]]

How can I do this multiplication to get tensor C as above?
Thanks

Hi,

It is a element wise multiplication which can be done using torch.mul(A, B). You just need to take care of shapes to have a proper broadcasting.


x = torch.tensor([[1,2,3],[4,5,6],[7,8,9]])
y = torch.tensor([1,10,100])

torch.mul(x, y.view(-1, 1))

Bests

It works.
Thank you.