# Array of arrays construction

**URL:** <https://discourse.julialang.org/t/array-of-arrays-construction/31096>\
**Category:** General Usage\
**Created:** [November 14, 2019, 7:01pm UTC](https://discourse.julialang.org/t/array-of-arrays-construction/31096 "2019-11-14T19:01:56Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![SingingKim](https://avatars.discourse-cdn.com/v4/letter/s/5fc32e/32.png) [@SingingKim](https://discourse.julialang.org/u/SingingKim)\
**Post date:** [November 14, 2019, 7:01pm UTC](https://discourse.julialang.org/t/array-of-arrays-construction/31096/1 "2019-11-14T19:01:56Z")

</div>

Hi,

The following is how I usually construct an array. Each element of array `temp` is an array that has different number of elements (which I assign later).

```julia
temp = Array{Array{Float64},1}(undef,10)
for ii in 1:10
   temp[ii] = Float64[]
end

```

Is there any way to reduce this code into one line? for example, by replacing `undef` operator by `Float64[]`, or anything like that?

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [November 14, 2019, 7:08pm UTC](https://discourse.julialang.org/t/array-of-arrays-construction/31096/2 "2019-11-14T19:08:13Z")

</div>

Use a comprehension.

```julia
[Float64[] for i in 1:10]

```

---

<div class="post-metadata">

**Author:** ![SingingKim](https://avatars.discourse-cdn.com/v4/letter/s/5fc32e/32.png) [@SingingKim](https://discourse.julialang.org/u/SingingKim)\
**Post date:** [November 14, 2019, 7:18pm UTC](https://discourse.julialang.org/t/array-of-arrays-construction/31096/3 "2019-11-14T19:18:47Z")

</div>

This is great! Thank you so much!

---

<div class="post-metadata">

**Author:** ![visr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/visr/32/17204_2.png) [@visr](https://discourse.julialang.org/u/visr)\
**Post date:** [November 14, 2019, 8:09pm UTC](https://discourse.julialang.org/t/array-of-arrays-construction/31096/4 "2019-11-14T20:09:09Z")

</div>

Or, if you want to save a few more characters

```julia
fill(Float64[], 10)

```

Edit: wrong, see below

---

<div class="post-metadata">

**Author:** ![non-Jedi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/non-jedi/32/3645_2.png) [@non-Jedi](https://discourse.julialang.org/u/non-Jedi)\
**Post date:** [November 14, 2019, 8:33pm UTC](https://discourse.julialang.org/t/array-of-arrays-construction/31096/5 "2019-11-14T20:33:23Z")

</div>

This fills the array with 10 references to the same empty array, which probably isn’t what you want. There’s a warning in the docstring about this I believe.

---

<div class="post-metadata">

**Author:** ![visr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/visr/32/17204_2.png) [@visr](https://discourse.julialang.org/u/visr)\
**Post date:** [November 14, 2019, 9:45pm UTC](https://discourse.julialang.org/t/array-of-arrays-construction/31096/6 "2019-11-14T21:45:35Z")

</div>

You’re right good catch, I retract my answer. There is indeed a warning about this in the docs.

---

<div class="post-metadata">

**Author:** ![SingingKim](https://avatars.discourse-cdn.com/v4/letter/s/5fc32e/32.png) [@SingingKim](https://discourse.julialang.org/u/SingingKim)\
**Post date:** [November 14, 2019, 10:05pm UTC](https://discourse.julialang.org/t/array-of-arrays-construction/31096/7 "2019-11-14T22:05:07Z")

</div>

One follow-up question.

```julia
[Float64[] for i in 1:10]

```

This gives me a vector of vectors (1-D array). How can I make a vector of 2-D arrays?

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [November 14, 2019, 10:19pm UTC](https://discourse.julialang.org/t/array-of-arrays-construction/31096/8 "2019-11-14T22:19:28Z")

</div>

Edit: Oh, well, that link is about matrix of vectors, which is interesting but not what was asked here.

See [Initialize array of arrays](https://discourse.julialang.org/t/initialize-array-of-arrays/11610).

---

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [November 14, 2019, 10:25pm UTC](https://discourse.julialang.org/t/array-of-arrays-construction/31096/9 "2019-11-14T22:25:20Z")

</div>

`Float64[]` is an alias for` Array{Float64,1}()`. So you can use` Array{Float64,2}(undef,r,c)`. It is necessary to specify undef and the dimensions for arrays with more than one dimension. Depending on your use case it might be simpler to use `x = [fill(0.0,2,2) for i in 1:10]`

---

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [November 14, 2019, 10:58pm UTC](https://discourse.julialang.org/t/array-of-arrays-construction/31096/10 "2019-11-14T22:58:20Z")

</div>

You can also initialize as follows if you do not want the sub arrays to be predefined.

```julia
x = Array{Array{Float64,2},1}(undef,2)

x[1] = rand(3,3)

x[2] = rand(2,2)
push!(x, rand(1,3))# push additional arrays

```

---

<div class="post-metadata">

**Author:** ![SingingKim](https://avatars.discourse-cdn.com/v4/letter/s/5fc32e/32.png) [@SingingKim](https://discourse.julialang.org/u/SingingKim)\
**Post date:** [November 15, 2019, 1:44am UTC](https://discourse.julialang.org/t/array-of-arrays-construction/31096/11 "2019-11-15T01:44:38Z")

</div>

Thanks! I appreciate your reply.
