Create an array of arrays

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?

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

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

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…

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 ?

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

OK so

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

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

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

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

It worked, thank you!

Welcome to Julia :wink:

I think you might be able to do the following:

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

1 Like