# Decomposing a tensor

Hi Pytorch Community
I want to decompose a tensor into some non_overlapping tensors. Let me show you an example of what i want to do. Assume that i have a tensor which it’s elements are only 1 like:

a = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]

Now i want to decompose it into a few non_overlapping tensors. here for simplicity we decompose them randomly into two tensors called b and c which their elements are only 1 and 0 as below:

b = [0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 1]
c = [1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0]

Do you have any suggestion on how to do it properly?
Thanks

Your question is not clear enough for me to understand. What exactly do you mean by “decomposing into a few non-overlapping tensors”? How many is “a few”? Is it always two? If not, how many?

Could describe what you expect the output to be like, when the input vector is:

``````a = [10, -5, 7, 8, 12, -15, 65, -1001, 78, 4]
``````
1 Like

Given your example, you might be looking for `scatter_`:

``````a = torch.tensor([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], dtype=torch.float32).unsqueeze(0)
b = torch.zeros(2, a.size(1))
idx = torch.randint(0, 2, (1, a.size(1)))
b.scatter_(0, idx, a)
print(b)
> tensor([[0., 0., 1., 1., 0., 0., 1., 1., 0., 0., 1.],
[1., 1., 0., 0., 1., 1., 0., 0., 1., 1., 0.]])
``````
1 Like

Thank you so much for replying.
That is exactly what i was looking for.