# Create an array of arrays

**URL:** <https://discourse.julialang.org/t/create-an-array-of-arrays/27550>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [August 14, 2019, 6:01pm UTC](https://discourse.julialang.org/t/create-an-array-of-arrays/27550 "2019-08-14T18:01:07Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fkazama](https://avatars.discourse-cdn.com/v4/letter/f/e79b87/32.png) [@Fkazama](https://discourse.julialang.org/u/Fkazama)\
**Post date:** [August 14, 2019, 6:01pm UTC](https://discourse.julialang.org/t/create-an-array-of-arrays/27550/1 "2019-08-14T18:01:07Z")

</div>

Hello,

I need to create an array of arrays, I am trying the following code:

"using JuMP

populacao=10  
individuo = Array{Array{Int64,2},1}

for a=1:populacao  
individuo[a]=rand{[0,1],2,3}  
println(individuo[a])  
end "

But the following error occurs:

“LoadError: TypeError: in Type{…} expression, expected UnionAll, got typeof(rand)”

What am I doing wrong? How is the right way to do that?

---

<div class="post-metadata">

**Author:** ![LaurentPlagne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laurentplagne/32/10103_2.png) [@LaurentPlagne](https://discourse.julialang.org/u/LaurentPlagne)\
**Post date:** [August 14, 2019, 6:04pm UTC](https://discourse.julialang.org/t/create-an-array-of-arrays/27550/2 "2019-08-14T18:04:33Z")

</div>

`individuo = Array{Array{Int64,2},1}(undef,populacao)`

---

<div class="post-metadata">

**Author:** ![Fkazama](https://avatars.discourse-cdn.com/v4/letter/f/e79b87/32.png) [@Fkazama](https://discourse.julialang.org/u/Fkazama)\
**Post date:** [August 14, 2019, 6:06pm UTC](https://discourse.julialang.org/t/create-an-array-of-arrays/27550/3 "2019-08-14T18:06:53Z")

</div>

I tried it, but the following error appeared:

LoadError: TypeError: in Type{…} expression, expected UnionAll, got typeof(rand)  
in expression starting at untitled-562d1f130b571f2022c82fcc98d20a43:6  
top-level scope at untitled-562d1f130b571f2022c82fcc98d20a43:7 [inlined]  
top-level scope at none:0

---

<div class="post-metadata">

**Author:** ![LaurentPlagne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laurentplagne/32/10103_2.png) [@LaurentPlagne](https://discourse.julialang.org/u/LaurentPlagne)\
**Post date:** [August 14, 2019, 6:09pm UTC](https://discourse.julialang.org/t/create-an-array-of-arrays/27550/4 "2019-08-14T18:09:48Z")

</div>

Would be better if you put your code between triple quote.  
The problem is `rand{[0,1],2,3}`  
You need parenthesis for function argument. I am not sure of what you want to achieve…

---

<div class="post-metadata">

**Author:** ![LaurentPlagne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laurentplagne/32/10103_2.png) [@LaurentPlagne](https://discourse.julialang.org/u/LaurentPlagne)\
**Post date:** [August 14, 2019, 6:11pm UTC](https://discourse.julialang.org/t/create-an-array-of-arrays/27550/5 "2019-08-14T18:11:34Z")

</div>

```julia
rand([0,1],10)
10-element Array{Int64,1}:
 1
 1
 0
 0
 0
 1
 1
 1
 1
 0

```

What are the 2,3 arguments ?

---

<div class="post-metadata">

**Author:** ![Fkazama](https://avatars.discourse-cdn.com/v4/letter/f/e79b87/32.png) [@Fkazama](https://discourse.julialang.org/u/Fkazama)\
**Post date:** [August 14, 2019, 6:27pm UTC](https://discourse.julialang.org/t/create-an-array-of-arrays/27550/6 "2019-08-14T18:27:33Z")

</div>

The arguments 2,3 is because each of my individuo[a] is a matrix 2x3 with 0 or 1 inside.

---

<div class="post-metadata">

**Author:** ![LaurentPlagne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laurentplagne/32/10103_2.png) [@LaurentPlagne](https://discourse.julialang.org/u/LaurentPlagne)\
**Post date:** [August 14, 2019, 6:30pm UTC](https://discourse.julialang.org/t/create-an-array-of-arrays/27550/7 "2019-08-14T18:30:01Z")

</div>

OK so

```julia
populacao=10
individuo = Array{Array{Int64,2},1}(undef,populacao)

for a=1:populacao
   individuo[a]=rand([0,1],2,3)
   println(individuo[a])
end

```

---

<div class="post-metadata">

**Author:** ![LaurentPlagne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laurentplagne/32/10103_2.png) [@LaurentPlagne](https://discourse.julialang.org/u/LaurentPlagne)\
**Post date:** [August 14, 2019, 6:32pm UTC](https://discourse.julialang.org/t/create-an-array-of-arrays/27550/8 "2019-08-14T18:32:49Z")

</div>

In case you don’t know the size in advance:

```julia
populacao=20
individuo = Array{Array{Int64,2},1}()

for a=1:populacao
   push!(individuo,rand([0,1],2,3))
   println(individuo[end])
end

```

---

<div class="post-metadata">

**Author:** ![Fkazama](https://avatars.discourse-cdn.com/v4/letter/f/e79b87/32.png) [@Fkazama](https://discourse.julialang.org/u/Fkazama)\
**Post date:** [August 14, 2019, 6:33pm UTC](https://discourse.julialang.org/t/create-an-array-of-arrays/27550/9 "2019-08-14T18:33:40Z")

</div>

It worked, thank you!

---

<div class="post-metadata">

**Author:** ![LaurentPlagne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laurentplagne/32/10103_2.png) [@LaurentPlagne](https://discourse.julialang.org/u/LaurentPlagne)\
**Post date:** [August 14, 2019, 6:34pm UTC](https://discourse.julialang.org/t/create-an-array-of-arrays/27550/10 "2019-08-14T18:34:07Z")

</div>

Welcome to Julia 😉

---

<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:** [August 14, 2019, 6:57pm UTC](https://discourse.julialang.org/t/create-an-array-of-arrays/27550/11 "2019-08-14T18:57:56Z")

</div>

I think you might be able to do the following:

`individuo = [rand(0:1,2,3) for _ in 1:10]`
