Replace part of the elements in the tensor, how to handle?

Hello everyone. I have recently come across a problem.
I have a tensor A and a tensor B. I want to paste B to A with some known position information.
The whole process can be described as follows.

I read some documentation about scatter_ or index_put, but I don’t know how to deal with this problem, because the position corresponding to each patch is irregular…

Based on your image you could either use direct indexing or e.g. masked_scatter_:

A = torch.zeros(3, 5, 5)
B = torch.randn(3, 2, 2)

A[0, 1:3, 1:3] = B[0]
A[1, 0:2, 3:5] = B[1]
A[2, 3:5, 0:2] = B[2]

A = torch.zeros(3, 5, 5)
mask = torch.zeros(3, 5, 5).bool()
mask[0, 1:3, 1:3] = True
mask[1, 0:2, 3:5] = True
mask[2, 3:5, 0:2] = True
A.masked_scatter_(mask, B)

I don’t know how the indices are created and which approach might be preferred.