# Convert 2×2 Array{Array{Float64,1},2} into Array{Float64, 3}

**URL:** <https://discourse.julialang.org/t/convert-2x2-array-array-float64-1-2-into-array-float64-3/52502>\
**Category:** New to Julia\
**Created:** [December 28, 2020, 10:43am UTC](https://discourse.julialang.org/t/convert-2x2-array-array-float64-1-2-into-array-float64-3/52502 "2020-12-28T10:43:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gxyd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gxyd/32/19986_2.png) [@gxyd](https://discourse.julialang.org/u/gxyd)\
**Post date:** [December 28, 2020, 10:43am UTC](https://discourse.julialang.org/t/convert-2x2-array-array-float64-1-2-into-array-float64-3/52502/1 "2020-12-28T10:43:40Z")

</div>

Dear all,

How do I convert an object of type `Array{Array{Float64,1},2}` to an object of type `Array{Float64, 2}`?

For ex.

```julia
> t = Array{Array{Float64,1}, 2}(undef, (2, 2))
> t[1] = [0., 0, 0];
> t[2] = [0., 0, 0];
> t[3] = [2., 2, 2];
> t[4] = [2., 2, 2];
> t
2×2 Array{Array{Float64,1},2}:
 [0.0, 0.0, 0.0] [2.0, 2.0, 2.0]
 [0.0, 0.0, 0.0] [2.0, 2.0, 2.0]

> t2 = Array{Float64, 3}(undef, 2, 2, 3)
> t2[1,1,1] = 0.
> t2[1,1,2] = 0.
> t2[1,1,3] = 0.
> t2[2,1,1] = 0.
> t2[2,1,2] = 0.
> t2[2,1,3] = 0.
> t2[1,2,1] = 2.
> t2[1,2,2] = 2.
> t2[1,2,3] = 2.
> t2[2,2,1] = 2.
> t2[2,2,2] = 2.
> t2[2,2,3] = 2.;
t2

2×2×3 Array{Float64,3}:
[:, :, 1] =
 0.0 2.0
 0.0 2.0

[:, :, 2] =
 0.0 2.0
 0.0 2.0

[:, :, 3] =
 0.0 2.0
 0.0 2.0

```

The steps to convert `t` to `t2`.

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [December 28, 2020, 10:59am UTC](https://discourse.julialang.org/t/convert-2x2-array-array-float64-1-2-into-array-float64-3/52502/2 "2020-12-28T10:59:02Z")

</div>

Unfortunately, I cannot copy and paste any code to reproduce your input, but you may try `reduce(hcat, X)` followed by an appropriate `reshape`

---

<div class="post-metadata">

**Author:** ![gxyd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gxyd/32/19986_2.png) [@gxyd](https://discourse.julialang.org/u/gxyd)\
**Post date:** [December 28, 2020, 11:18am UTC](https://discourse.julialang.org/t/convert-2x2-array-array-float64-1-2-into-array-float64-3/52502/3 "2020-12-28T11:18:51Z")

</div>

> [@gxyd](#):
>
> The steps to convert `X` to `Y` .

I can do that, but the thing the ordering of values is not what I expect. Using `reshape(reduce(hcat, X), (2, 2, 3))` I get:

```julia
2×2×3 Array{Float64,3}:
[:, :, 1] =
 0.0 0.0
 0.0 0.0

[:, :, 2] =
 0.0 2.0
 0.0 2.0

[:, :, 3] =
 2.0 2.0
 2.0 2.0

```

which isn’t the expected outcome.

---

<div class="post-metadata">

**Author:** ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Post date:** [December 28, 2020, 11:30am UTC](https://discourse.julialang.org/t/convert-2x2-array-array-float64-1-2-into-array-float64-3/52502/4 "2020-12-28T11:30:24Z")

</div>

Transpose the result of `reduce(hcat, X)` and you’ll have it it:

```julia
julia> reduce(hcat, X) |>
       a -> permutedims(a, (2,1)) |>
       a -> reshape(a, (2,2,3))
2×2×3 Array{Float64,3}:
[:, :, 1] =
 0.0 2.0
 0.0 2.0

[:, :, 2] =
 0.0 2.0
 0.0 2.0

[:, :, 3] =
 0.0 2.0
 0.0 2.0

```
