# How to convert this 3d matrix to 2d efficiently?

**URL:** <https://discuss.pytorch.org/t/how-to-convert-this-3d-matrix-to-2d-efficiently/119845>\
**Category:** Uncategorized\
**Created:** [April 29, 2021, 5:32pm UTC](https://discuss.pytorch.org/t/how-to-convert-this-3d-matrix-to-2d-efficiently/119845 "2021-04-29T17:32:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ADONAI\_TZEVAOT](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/adonai_tzevaot/32/31089_2.png) [@ADONAI\_TZEVAOT](https://discuss.pytorch.org/u/ADONAI_TZEVAOT)\
**Post date:** [April 29, 2021, 5:32pm UTC](https://discuss.pytorch.org/t/how-to-convert-this-3d-matrix-to-2d-efficiently/119845/1 "2021-04-29T17:32:11Z")

</div>

I have a matrix A which is 3d and I want to convert so that it is equal to B which is a 2d matrix

```auto
> A = torch.tensor( [[[1,1,1,1,1],
> [2,2,2,2,2],
> [3,3,3,3,3],
> [4,4,4,4,4],
> [5,5,5,5,5]],
> 
> [[6,6,6,6,6]
> [7,7,7,7,7],
> [8,8,8,8,8],
> [9,9,9,9,9],
> [10,10,10,10,10]],
> 
> [[11,11,11,11,11],
> [12,12,12,12,12],
> [13,13,13,13,13],
> [14,14,14,14,14],
> [15,15,15,15,15]]])
> 
> B = torch.tensor( [[1,1,1,1,1, 6,6,6,6,6, 11,11,11,11,11],
> [2,2,2,2,2, 7,7,7,7,7, 12,12,12,12,12],
> [3,3,3,3,3, 8,8,8,8,8, 13,13,13,13,13],
> [4,4,4,4,4, 9,9,9,9,9, 14,14,14,14,14],
> [5,5,5,5,5, 10,10,10,10,10, 15,15,15,15,15]])

```

Please help

---

<div class="post-metadata">

**Author:** ![Mole\_Turner](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/mole_turner/32/37337_2.png) [@Mole\_Turner](https://discuss.pytorch.org/u/Mole_Turner)\
**Post date:** [April 29, 2021, 6:01pm UTC](https://discuss.pytorch.org/t/how-to-convert-this-3d-matrix-to-2d-efficiently/119845/2 "2021-04-29T18:01:27Z")

</div>

I’m really awful at working out dimensions from printing out arrays. But if I’m right `A.shape = (5,5,3) and B.shape = (5,15)?` If this is the case could you not just do `A = torch.reshape(A,B.shape())`

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [May 1, 2021, 9:44am UTC](https://discuss.pytorch.org/t/how-to-convert-this-3d-matrix-to-2d-efficiently/119845/3 "2021-05-01T09:44:24Z")

</div>

Directly reshaping `A` will unfortunately not work, as it would result in:

```python
print(A.view(B.size()))
> tensor([[1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3],
          [4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6],
          [7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9],
          [10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12],
          [13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15]])

```

To get the desired output you would need to `permute` the first two dimensions first:

```python
print(A.permute(1, 0, 2).reshape(B.size()))
> tensor([[1, 1, 1, 1, 1, 6, 6, 6, 6, 6, 11, 11, 11, 11, 11],
          [2, 2, 2, 2, 2, 7, 7, 7, 7, 7, 12, 12, 12, 12, 12],
          [3, 3, 3, 3, 3, 8, 8, 8, 8, 8, 13, 13, 13, 13, 13],
          [4, 4, 4, 4, 4, 9, 9, 9, 9, 9, 14, 14, 14, 14, 14],
          [5, 5, 5, 5, 5, 10, 10, 10, 10, 10, 15, 15, 15, 15, 15]])

```
