Initialization of an empty array, which will be filled in a loop with arrays of different sizes

Hello,

I would like to programm an algorithm, where an array will be filled with other arrays of different sizes. Does somebody know how to it?

Best regards

Probably yes.

But you have to be more specific, at provide at least

  1. an example of desired inputs and corresponding outputs,

  2. some code that you have tried and need help with.

1 Like

Ok , thanks that makes sense. I didn´t know this. I found a solution, which works for me. I use an array of dicts

all_soultions = [Dict("solutions" => []) for i in 1:10]
for i in 1:length(all_soultions)
    if i==1
        all_soultions[i]["solutions"] = [1,2,3]
    elseif i ==2
        all_soultions[i]["solutions"] = [1,2]
    elseif i==3
        all_soultions[i]["solutions"] = [1,2,3,4]
    elseif i ==4
        all_soultions[i]["solutions"] = [2]
    end
end

What about

all_soultions = Vector{Vector{Int}}(undef, 10)
for i in 1:length(all_soultions)
    if i==1
        all_soultions[i] = [1,2,3]
    elseif i ==2
        all_soultions[i] = [1,2]
    elseif i==3
        all_soultions[i] = [1,2,3,4]
    elseif i ==4
        all_soultions[i] = [2]
    end
end

Or maybe more appropriate depending on your use-case:

all_soultions = Vector{Int}[]
for i in 1:4
    if i==1
        push!(all_soultions, [1,2,3])
    elseif i ==2
        push!(all_soultions, [1,2])
    elseif i==3
        push!(all_soultions, [1,2,3,4])
    elseif i ==4
        push!(all_soultions, [2])
    end
end
1 Like

I don’t quite see why you need the Dicts here, you are just adding a layer of indirection for no reason apparent in your example.

1 Like

You are right. I just started to use Julia a few days ago. So I´m a little bit unfamiliar with the syntax and have to learn such things. Thanks both of you for your help.

2 Likes