# Assigning values to a tensor after comparison between two other tensors

I have two 2d tensors like this:

``````x = tensor([[1311, 1297, 3872, 1299, 1445, 1299, 3876, 1298,    2],
[1316, 1301, 3872, 1804, 1326, 3875, 1378, 1298,    2] ]])
y = tensor([[1311, 1297,    1, 1299, 1445, 1299,    2, 1298,    2],
[1316, 1301,    1, 1804, 1326,    2, 1378, 1298,    2] ]])
``````

The tensors differ from each other at some indices and I want another tensor to be updated based on the values in these tensors at differing positions. i.e.,

``````z= tensor([[   2,   16,   17,   10,   12,   13, 3876, 3872,   10,   14],
[   2,   16,   17,   10,   12,   13, 3875, 3872,   10,   14]]])
``````

z should become:

``````z= tensor([[   2,   16,   17,   10,   12,   13, 2, 1,   10,   14],
[   2,   16,   17,   10,   12,   13, 2, 1,   10,   14]]])
``````

How can this be done?

I don’t understand how `z` is created in your example.
Based on `x` and `y`, the indices of the different values would be:

``````(x != y).nonzero()
> tensor([[0, 2],
[0, 6],
[1, 2],
[1, 5]])
``````

However, `z` seems to be changed at columns 6 and 7 with the values 2 and 1?
Could you explain how these values were set?

z is a tensor which has some values. If z has some value > 3870 in my case, we need to go the tensor x, and find its index. If the value is 3872, the index in tensor x is: [0][2] and its corresponding value in y tensor is 1 i.e., at index [0][2]. We need to substitute 3872’s value in z by corresponding value in y tensor. So, z[0][7] which had value 3872 becomes: 1.