tonythestark
tonythestark

Reputation: 543

Julia : Initiliase a vector of an array of vectors

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?

Upvotes: 1

Views: 663

Answers (2)

AboAmmar
AboAmmar

Reputation: 5559

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]
 []
 []
 []

Upvotes: 3

Oscar Smith
Oscar Smith

Reputation: 6378

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.

Upvotes: 2

Related Questions