# Nested Vectors/Arrays to multidimensional tensor

**URL:** https://discourse.julialang.org/t/nested-vectors-arrays-to-multidimensional-tensor/36186
**Category:** General Usage
**Created:** [March 19, 2020, 2:23pm UTC](https://discourse.julialang.org/t/nested-vectors-arrays-to-multidimensional-tensor/36186 "2020-03-19T14:23:07Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![LudiWin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ludiwin/32/9163_2.png) [@LudiWin](https://discourse.julialang.org/u/LudiWin)
#### Post date: [March 19, 2020, 2:23pm UTC](https://discourse.julialang.org/t/nested-vectors-arrays-to-multidimensional-tensor/36186/1 "2020-03-19T14:23:07Z")

</div>

I searched far and wide in the Julia forum and stackoverflow but haven’t found a general answer to my question (or I missed the point of the answers):  
How can I collect an arbitrary number of nested vectors/arrays into a single multi dimensional tensor?

A minimum working example that generates a similar set of data is the following  
`data = [[rand(13,14) for i in 1:12] for j in 1:11]`  
`data representation: Vector{Array{Array{Float64,2},1}`

How can I transform this into a single 4 dimensional tensor of the shape `(11, 12, 13, 14)`?

Obviously I could write basically boiler plate code on my own but I was wondering whether there is some function/package that does that for me.

The application is computational statistics where I want to manipulate the data tensors along its dimensions directly thus circumventing onerous for-loops.

Thanks for any help in advance! =)

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [March 19, 2020, 2:59pm UTC](https://discourse.julialang.org/t/nested-vectors-arrays-to-multidimensional-tensor/36186/2 "2020-03-19T14:59:54Z")

</div>

Try something like

```julia
data = [[rand(13,14) for i in 1:12] for j in 1:11]
A = permutedims(reshape(mapreduce(xs -> vec(reduce(hcat, xs)), hcat, data),
                        13, 14, 12, :),
                (4, 3, 1, 2))
data[1][1] == A[1, 1, :, :] # true

```

---

<div class="post-metadata">

### Author: ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)
#### Post date: [March 19, 2020, 3:23pm UTC](https://discourse.julialang.org/t/nested-vectors-arrays-to-multidimensional-tensor/36186/3 "2020-03-19T15:23:12Z")

</div>

I have some packages which may interest you (one [just announced](https://discourse.julialang.org/t/ann-lazystack-slicemap-starslice/36072) this week):

```julia
julia> data = [[rand(13,14) for i in 1:12] for j in 1:11];

julia> using LazyStack 

julia> stack(stack.(data)) |> size # this is a view, copy for Array
(13, 14, 12, 11)

julia> permutedims(stack(stack.(data)), (4,3,1,2)) |> size
(11, 12, 13, 14)

julia> using TensorCast

julia> size(@cast out[a,b,c,d] := data[a][b][c,d])
(11, 12, 13, 14)

```

---

<div class="post-metadata">

### Author: ![LudiWin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ludiwin/32/9163_2.png) [@LudiWin](https://discourse.julialang.org/u/LudiWin)
#### Post date: [March 19, 2020, 4:39pm UTC](https://discourse.julialang.org/t/nested-vectors-arrays-to-multidimensional-tensor/36186/4 "2020-03-19T16:39:56Z")

</div>

Thank you for your replies.

I will have a look into the answers. =)

---

<div class="post-metadata">

### Author: ![LudiWin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ludiwin/32/9163_2.png) [@LudiWin](https://discourse.julialang.org/u/LudiWin)
#### Post date: [May 26, 2020, 11:27am UTC](https://discourse.julialang.org/t/nested-vectors-arrays-to-multidimensional-tensor/36186/5 "2020-05-26T11:27:50Z")

</div>

Ultimately, I rewrote the methods and worked explicitly with multi-dimensional arrays and `cat(array1, array2)` and from the start instead of nested arrays.

---

<div class="post-metadata">

### Author: ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)
#### Post date: [May 26, 2020, 3:06pm UTC](https://discourse.julialang.org/t/nested-vectors-arrays-to-multidimensional-tensor/36186/6 "2020-05-26T15:06:19Z")

</div>

I find [https://github.com/JuliaData/SplitApplyCombine.jl](https://github.com/JuliaData/SplitApplyCombine.jl) really useful for this kind of problems, and routinely use its functions. There are both copying and non-copying (i.e. `view`) versions.
