# Scatter vectors [Solved]

``````num_vectors = 5
max_length = 3
num_out = 3
t = torch.arange(num_vectors).view(-1, 1).float()
zeros = torch.zeros(num_out * max_length, 1)

zeros[[0, 1, 2, 3, 6, 7]] = t[[2, 1, 0, 4, 3, 2]]
print(zeros.view(3, 3, 1))
``````
``````tensor([[[2.],
[1.],
[0.]],

[[4.],
[0.],
[0.]],

[[3.],
[2.],
[0.]]])
``````

OLD POST:
Hey,

I want to scatter vectors according to a list of indices of variable lengths while padding with zeros like this:

``````vectors = torch.tensor(
[
[5],
[6],
[7]
]
)
# vectors.size(): (num_vectors, vector_size) = (3, 1)

indices = [[1, 2], [0], [0, 1, 1, 1]]

# scattered:
# torch.tensor(
#     [
#         [[6], [7], [0], [0]],
#         [[5], [0], [0], [0]],
#         [[5], [6], [6], [6]],
#     ]
# )
``````

I am currently using `tensor._scatter` like this:

``````def padded_scatter(src, indices, padding=0):
num_items = [len(idx) for idx in indices]
n_max = max(num_items)
out = torch.zeros(
n_max * len(num_items),
*src.size()[1:],
dtype=src.dtype,
device=src.device
)