Concat tensors row column

Hey,

I am wondering if there’s a more efficient method using pytorch to concat 2 tensors together of different dimensions.

Suppose

Tensor A shape: 1, 2
Tensor B shape: 4, 1

I want to concat these together to make

Tensor A_B: 4, 3

Where Tensor A 2nd dimension row is copied 4 times, with Tensor B’s respective second dimension value at the end of each row.

In other words, I want to do this:
A: [ 1, 2 ]

B: [ 1 ],
[ 2 ],
[ 3 ],
[ 4 ]

A_B: [ 1, 2, 1 ],
[ 1, 2, 2 ],
[ 1, 2, 3 ],
[ 1, 2, 4 ],

I know how to do this with loops, but it takes time with large datasets, I am wondering if it’s possible to do with a built in function.

Thanks!

This should work:

A = torch.tensor([1, 2])
B = torch.tensor([[1], [2], [3], [4]])
C = torch.cat((A.expand(B.size(0), -1), B), dim=1)