I have a two dimensional tensor A and a list of indices b. I want to assign the sub-matrix defined by the indices in b to some other values, contains in a tensor c.
If the indices b are contiguous, this is easy. For example suppose b contains indices from 0 to 3, then I can do:
>>> a = torch.zeros(5, 5)
>>> c = torch.ones(3, 3)
>>> a[0:3, 0:3] = c
>>> a
1 1 1 0 0
1 1 1 0 0
1 1 1 0 0
0 0 0 0 0
0 0 0 0 0
[torch.FloatTensor of size 5x5]
which is what I want. But what if the indices in b are not contiguous? That is, what is b = [0, 2, 4] or b=[0,2,3]?
Doing a[b, b] returns something that is not the sub-matrix I thought. Right now I am doing
>>> b = [0, 2, 4]
>>> for i in range(len(b)):
... for j in range(len(b)):
... a[b[i], b[j]] = c[i, j]
...
>>> a
1 0 1 0 1
0 0 0 0 0
1 0 1 0 1
0 0 0 0 0
1 0 1 0 1
[torch.FloatTensor of size 5x5]
So if you want to select the submatrix given rows and columns, with possibly non continuous indexes and not evenly spaced, the clearest way I found is generating the indexes using np.ix_.