Question about how to implement scatter a tensor

Hi, I have would like to implement such a requirement: a is a tensor(3,4), b is a tensor(5,4), mask is a tensor = [1, 0, 1, 0, 1]. How to put a to b according to the mask where is 1? That is b[0] = a[0], b[2] = a[1], b[3] = a[2]. Thank you very much!

You can achieve this by using a boolean mask rather than your numeric mask (PyTorch, NumPy, etc implements different behaviors depending on the data type in the arrays). For your example you would have to do

import torch 

a = torch.arange(3*4).reshape(3,4)
b = torch.zeros(5,4, dtype=torch.long)
mask = torch.tensor([1,0,1,0,1])
b[mask.bool()] = a
print(b)
# tensor([[ 0,  1,  2,  3],
#         [ 0,  0,  0,  0],
#         [ 4,  5,  6,  7],
#         [ 0,  0,  0,  0],
#         [ 8,  9, 10, 11]])