# Reshaping Tensor: Basic

**URL:** <https://discuss.pytorch.org/t/reshaping-tensor-basic/76971>\
**Category:** Uncategorized\
**Created:** [April 16, 2020, 2:37am UTC](https://discuss.pytorch.org/t/reshaping-tensor-basic/76971 "2020-04-16T02:37:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rana\_Fida](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/rana_fida/32/11836_2.png) [@Rana\_Fida](https://discuss.pytorch.org/u/Rana_Fida)\
**Post date:** [April 16, 2020, 2:37am UTC](https://discuss.pytorch.org/t/reshaping-tensor-basic/76971/1 "2020-04-16T02:37:56Z")

</div>

Hi,  
If we have a linear tensor of 24 elements from 0 to 23.  
X = [0 1 2 3 …23]  
How can we reshape it to something like that  
Y = [[0,1 2 3 12 13 14 15],  
[4 5 6 7 16 17 18 19],  
[8 9 10 11 20 21 22 23]]

Thanks

---

<div class="post-metadata">

**Author:** ![abodh\_ltd](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/abodh_ltd/32/22637_2.png) [@abodh\_ltd](https://discuss.pytorch.org/u/abodh_ltd)\
**Post date:** [April 16, 2020, 2:59am UTC](https://discuss.pytorch.org/t/reshaping-tensor-basic/76971/2 "2020-04-16T02:59:06Z")

</div>

```python
x = torch.range(1,24)
print (x)
x = x.view(3,-1)
print (x)

Output:
tensor([[1., 2., 3., 4., 5., 6., 7., 8.],
        [9., 10., 11., 12., 13., 14., 15., 16.],
        [17., 18., 19., 20., 21., 22., 23., 24.]])

```

---

<div class="post-metadata">

**Author:** ![Rana\_Fida](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/rana_fida/32/11836_2.png) [@Rana\_Fida](https://discuss.pytorch.org/u/Rana_Fida)\
**Post date:** [April 16, 2020, 3:10am UTC](https://discuss.pytorch.org/t/reshaping-tensor-basic/76971/3 "2020-04-16T03:10:04Z")

</div>

Well The issue is we don’t want it [1 2 3 4 5 6 7 8]  
we want it to be [0 1 2 3 **12 13 14 15**]  
and so on… there is gap. See expected output given in the question

---

<div class="post-metadata">

**Author:** ![vdw](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/vdw/32/10074_2.png) [@vdw](https://discuss.pytorch.org/u/vdw)\
**Post date:** [April 16, 2020, 3:26am UTC](https://discuss.pytorch.org/t/reshaping-tensor-basic/76971/4 "2020-04-16T03:26:45Z")

</div>

Well you can first split your original tensor in half, reshape both halves and concatenate them again:

```
# initilizing list 
X = list(range(0, 24))

# converting list to array 
X = np.array(X) 

# converting array to tensor
X = torch.from_numpy(X)

# split tensor in half
X_split = torch.split(X, int(X.shape[0]/2), dim=0)

# reshape the two tensors
X_left = X_split[0].view(3,-1)
X_right = X_split[1].view(3,-1)

# concatenate the reshaped tensors again
X = torch.cat((X_left, X_right), dim=1)

```

That wil give you:

```
tensor([[0, 1, 2, 3, 12, 13, 14, 15],
        [4, 5, 6, 7, 16, 17, 18, 19],
        [8, 9, 10, 11, 20, 21, 22, 23]])
```

---

<div class="post-metadata">

**Author:** ![abodh\_ltd](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/abodh_ltd/32/22637_2.png) [@abodh\_ltd](https://discuss.pytorch.org/u/abodh_ltd)\
**Post date:** [April 16, 2020, 3:30am UTC](https://discuss.pytorch.org/t/reshaping-tensor-basic/76971/5 "2020-04-16T03:30:02Z")

</div>

Ohh I didn’t notice. Sorry about that. Chris (@vdw) has just posted a solution. I was about to write the same. See if that works for you.
