How to assign a tensor to another tensor at different rows and columns?

For example, I have a tensor

a = tensor([[ 0,  1,  2,  3,  4],
        [ 5,  6,  7,  8,  9],
        [10, 11, 12, 13, 14],
        [15, 16, 17, 18, 19]])

indices matrixes are b = torch.LongTensor([0, 2, 3]) and c = torch.LongTensor([2, 4])

I can get the d = a[b,:][:,c], but how can I assign the -d to a[b,:][:,c]?

Furthermore, how can I assign the tensor having the same dimensions as d to a[b,:][:,c]?

I assume a[b,:][:,c] represents the working reference and you would like to assign a new value to these indexed values?
If so, this would work:

a = torch.tensor([[ 0,  1,  2,  3,  4],
                  [ 5,  6,  7,  8,  9],
                  [10, 11, 12, 13, 14],
                  [15, 16, 17, 18, 19]])

b = torch.tensor([0, 2, 3])
c = torch.tensor([2, 4])

a[b.unsqueeze(1), c] = 1000
print(a)
# tensor([[   0,    1, 1000,    3, 1000],
#         [   5,    6,    7,    8,    9],
#         [  10,   11, 1000,   13, 1000],
#         [  15,   16, 1000,   18, 1000]])

Thanks for your reply :slight_smile:

I found it works even if a[b.unsqueeze(1), c] = d (where d is a 3x2 tensor)!
e.g.,

d = tensor([[ -2,  -4],
        [-12, -14],
        [-17, -19]])

Thank you very much :slight_smile: