Home > Enterprise >  Julia : Initiliase a vector of an array of vectors
Julia : Initiliase a vector of an array of vectors

Time:12-15

So I defined:

v =Array{Vector{Int64}, 5}

Now I want v[1] to be the empty vector.
I tried:
v[1] = Int64[] ,
v[1]=Vector{Int64}()
It does not work. How can I access this vector and fill it with a value?

CodePudding user response:

You want v =Array{Vector{Int64}, 5}(undef, s1, s2, s3, s4, s5) (where the ss are the size of each dimension) otherwise, v is the type, not an object of that type.

CodePudding user response:

This v = Array{Vector{Int64}, 5} is a type not an instance. You can use some of these options:

julia> v = Vector{Vector{Int}}(undef,5);

julia> v[1] = Int[];

julia> v[2] = [1,2,3];

julia> v
5-element Vector{Vector{Int64}}:
    []
    [1, 2, 3]
 #undef
 #undef
 #undef

Or if you don't mind starting with empty vectors, this can be simpler:

julia> v = [Int[] for i=1:5];

julia> v[2] = [1,2,3];

julia> v
5-element Vector{Vector{Int64}}:
 []
 [1, 2, 3]
 []
 []
 []
  • Related