# Array of arrays to a single nxn dimensional array

**URL:** <https://discourse.julialang.org/t/array-of-arrays-to-a-single-nxn-dimensional-array/15753>\
**Category:** New to Julia\
**Created:** [October 1, 2018, 6:21pm UTC](https://discourse.julialang.org/t/array-of-arrays-to-a-single-nxn-dimensional-array/15753 "2018-10-01T18:21:12Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Phillip\_Sutton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phillip_sutton/32/7603_2.png) [@Phillip\_Sutton](https://discourse.julialang.org/u/Phillip_Sutton)\
**Post date:** [October 1, 2018, 6:21pm UTC](https://discourse.julialang.org/t/array-of-arrays-to-a-single-nxn-dimensional-array/15753/1 "2018-10-01T18:21:13Z")

</div>

I need to convert an array of nx1 arrays to an nxn array. I have Array{Array{Float64,2},1}. Is this a reshape use case?

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [October 1, 2018, 6:25pm UTC](https://discourse.julialang.org/t/array-of-arrays-to-a-single-nxn-dimensional-array/15753/2 "2018-10-01T18:25:36Z")

</div>

If `n` is relatively small, you can write

```julia
hcat(A...)

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [October 1, 2018, 6:35pm UTC](https://discourse.julialang.org/t/array-of-arrays-to-a-single-nxn-dimensional-array/15753/3 "2018-10-01T18:35:27Z")

</div>

Though it seems like this is better and faster:

```julia
reduce(hcat, A)

```

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [October 1, 2018, 11:42pm UTC](https://discourse.julialang.org/t/array-of-arrays-to-a-single-nxn-dimensional-array/15753/4 "2018-10-01T23:42:42Z")

</div>

Though `reduce` will work as expected, I think `foldl` gives the proper guarantees.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [October 2, 2018, 6:11am UTC](https://discourse.julialang.org/t/array-of-arrays-to-a-single-nxn-dimensional-array/15753/5 "2018-10-02T06:11:40Z")

</div>

`foldl` seems _significantly_ slower, though. What are the advantages/guarantees that `foldl` offers?

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [October 2, 2018, 7:59am UTC](https://discourse.julialang.org/t/array-of-arrays-to-a-single-nxn-dimensional-array/15753/6 "2018-10-02T07:59:30Z")

</div>

That’s interesting. I am getting a 9x slowdown with `foldl`. AFAIU, `foldl` is guaranteed to go through the elements from left to right, or perhaps more correctly start to end, with the accumulated result always as the first argument of the function. On the other hand, from the `reduce` docs:

```julia
 The associativity of the reduction is implementation-dependent; if you need a particular associativity, e.g.
  left-to-right, you should write your own loop or consider using foldl or foldr. See documentation for reduce.

```

But the slowdown is surprising and perhaps buggy.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [October 2, 2018, 12:05pm UTC](https://discourse.julialang.org/t/array-of-arrays-to-a-single-nxn-dimensional-array/15753/7 "2018-10-02T12:05:09Z")

</div>

I suspect `foldl`’s guaranteed associativity means that it has to allocate a new matrix for the result of `hcat` for every single column. That’s unnecessary and wasteful, but it’s required by the guarantee that foldl makes. Since `reduce` makes no such guarantee, it is free to take a faster path and avoid the extra copies.

Edit: see [Problem with cat() - #9 by nalimilan](https://discourse.julialang.org/t/problem-with-cat/15750/9)

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [October 2, 2018, 12:20pm UTC](https://discourse.julialang.org/t/array-of-arrays-to-a-single-nxn-dimensional-array/15753/8 "2018-10-02T12:20:27Z")

</div>

Thanks for the pointer. But arguably, the same optimization can be applied to `foldl` since allocating once seems orthogonal to associativity, I don’t see how they are related.
