Generate a sub-2D tensor by reference

Dear all,

I would like to inquire whether it is possible to generate a sub-matrix (2D, torch::Tensor) from a large matrix (torch::Tensor) using a list of row and column indices, such that the sub-matrix serves as a reference to the original matrix rather than a copy. Specifically, I am looking for a solution where any changes made to the sub-matrix directly reflect in the original matrix.

Best, Weitao.

Yes, this is possible if a view can be created as seen here:

a = torch.zeros(8, 8)
b = a[2:4, 2:4]
b.fill_(1.)

print(a)
# tensor([[0., 0., 0., 0., 0., 0., 0., 0.],
#         [0., 0., 0., 0., 0., 0., 0., 0.],
#         [0., 0., 1., 1., 0., 0., 0., 0.],
#         [0., 0., 1., 1., 0., 0., 0., 0.],
#         [0., 0., 0., 0., 0., 0., 0., 0.],
#         [0., 0., 0., 0., 0., 0., 0., 0.],
#         [0., 0., 0., 0., 0., 0., 0., 0.],
#         [0., 0., 0., 0., 0., 0., 0., 0.]])

However, not all passed indices can create views:

c = a[[0, 4, 7], [0, 2, 3]]
c.fill_(2.)

print(a)