# Update one tensor column from other tensor

I have tensor ts1 and ts2 , I want to update ts1 last column from value of second column of ts2 on the base of condition ts2[:,1]==ts1[:,0] , but without loop, thankyou , any help

``````ts3=torch.tensor([
[ 24,  24,   8,0 ],
[ 38 , 100 , 93,0],
[ 65 , 91 , 73 ,0],
[ 49 , 26 , 45 ,0],
[ 78 , 54 , 11 ,0],
[ 24 , 98 , 12 ,0],
[ 27 , 55 , 50 ,0],
[ 11 , 65 , 96 ,0],
[ 45 , 40 , 16 ,0],
[ 99 , 54 , 27,0 ]],dtype=torch.int32)

ts2=torch.tensor([[100,10],[26,20],[54,30]],dtype=torch.int32)

the result should be

([
[ 24,  24,   8,0 ],
[ 38 , 100 , 93,10],
[ 65 , 91 , 73 ,0],
[ 49 , 26 , 45 ,20],
[ 78 , 54 , 11 ,30],
[ 24 , 98 , 12 ,0],
[ 27 , 55 , 50 ,0],
[ 11 , 65 , 96 ,0],
[ 45 , 40 , 16 ,0],
[ 99 , 54 , 27,30 ]])

``````

I spent a little time trying to find something similar to a “Join on value”, but couldn’t come up with anything…

This is the best I could come up with:

``````import torch

ts3=torch.tensor([
[ 24 , 24 , 8 ,0 ],
[ 38 , 100 , 93 ,0],
[ 65 , 91 , 73 ,0],
[ 49 , 26 , 45 ,0],
[ 78 , 54 , 11 ,0],
[ 24 , 98 , 12 ,0],
[ 27 , 55 , 50 ,0],
[ 11 , 65 , 96 ,0],
[ 45 , 40 , 16 ,0],
[ 99 , 54 , 27 ,0]],dtype=torch.int32)

ts2=torch.tensor([[100,10],[26,20],[54,30]],dtype=torch.int32)

for x, y in ts2[:]:
ts3[:, 3] = torch.where(ts3[:, 1] == x, y, ts3[:, 3])
``````

I know you didn’t want a loop, but this may help cut down on the amount of looping you were originally thinking there would be.

I’m still pretty new to Pytorch, so there is likely still a better way.

thank you callahman,
I want without loop !,