Concat tensor but mixing order

``````import torch

import numpy as np

x = torch.rand(4,4)

y = torch.rand(4,4)

print(x)

print(y)

print(torch.cat((x,y),0))
``````

If I execute this code it will be like

``````tensor([[0.1211, 0.7577, 0.8376, 0.4488],
[0.6628, 0.8632, 0.9736, 0.8368],
[0.5848, 0.0872, 0.0469, 0.2834],
[0.8561, 0.6229, 0.3667, 0.1358]])
tensor([[0.1820, 0.8955, 0.3811, 0.5496],
[0.5745, 0.3199, 0.5228, 0.8269],
[0.8408, 0.6984, 0.7248, 0.1973],
[0.3045, 0.9651, 0.8701, 0.3180]])
tensor([[0.1211, 0.7577, 0.8376, 0.4488],
[0.6628, 0.8632, 0.9736, 0.8368],
[0.5848, 0.0872, 0.0469, 0.2834],
[0.8561, 0.6229, 0.3667, 0.1358],
[0.1820, 0.8955, 0.3811, 0.5496],
[0.5745, 0.3199, 0.5228, 0.8269],
[0.8408, 0.6984, 0.7248, 0.1973],
[0.3045, 0.9651, 0.8701, 0.3180]])
``````

is there a way that I coult concat like

tensor([[0.1211, 0.7577, 0.8376, 0.4488],
[[0.1820, 0.8955, 0.3811, 0.5496],
[0.6628, 0.8632, 0.9736, 0.8368],

which is x row 1 comes first
and y row 1 and x row 2 and so on

I think I solved it

``````for  k in range(x.shape[0]):
if test is None:
test = torch.cat((z[k:k+1],z[k + x.shape[0]:k + x.shape[0]+1]),0)
print(test)
else:
test = torch.cat((test,z[k:k+1],z[k + x.shape[0]:k + x.shape[0]+1]),0)
``````

I think this does the job

thank you

Alternatively to the loop, you could also interleave the tensors directly as seen here:

``````x = torch.rand(4,4)
y = torch.rand(4,4)
print(x)
print(y)

z = torch.cat((x,y),0)
test = None
for  k in range(x.shape[0]):
if test is None:
test = torch.cat((z[k:k+1],z[k + x.shape[0]:k + x.shape[0]+1]),0)
print(test)
else:
test = torch.cat((test,z[k:k+1],z[k + x.shape[0]:k + x.shape[0]+1]),0)
print(test)

res = torch.cat((x.unsqueeze(1), y.unsqueeze(1)), 1).view(8, 4)
print((test == res).all())
> tensor(True)
``````