How 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?

Hi! welcome to discourse!

this works:

using JuMP

populacao=10
individuo = Array{Array{Int64,2},1}(undef,populacao) #creates an array of length 'populacao'

for a=1:populacao
individuo[a]=rand([0,1],2,3) #for what i can suppose, 
#you want a 2x3 matrix of random numbers 0 or 1
println(individuo[a])
end 

for last, i recommend you to read those guidelines when posting questions:(PSA: make it easier to help you)

1 Like

In the line individuo[a]=rand{[0,1],2,3} you’re using brackets instead of parenthesis to call the rand function.

Also, the way you’re defining individuo makes it a DataType, not an empty array.

I think that what you want is the following:

using Random

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

The last line is a list comprehension (not available in Matlab as far as I know, if you’re coming from that language).

1 Like