How to have multidimensional array with different length in Julia

Viewed 1211

I need to make a sequence of an array with different length by reading a dataset. I need to call each of them in a loop so probably I need some sort of indexing in order to call them. For example, how can I create the following sequence:

P[1]=[1 2 3 4]
P[2]=[1 4]
P[3]=[8 9 0 0 5 6]
.
.
.
1 Answers

Here it is:

julia> P = Vector{Vector{Int64}}([[1,2,3,4],[1,4],[8,9,0,0,5,6]])  
3-element Array{Array{Int64,1},1}:                                 
 [1, 2, 3, 4]                                                      
 [1, 4]                                                            
 [8, 9, 0, 0, 5, 6]                                                

julia> P[1]                                                        
4-element Array{Int64,1}:                                          
 1                                                                 
 2                                                                 
 3                                                                 
 4                                                                 

julia> P[2]                                                        
2-element Array{Int64,1}:                                          
 1                                                                 
 4                                                                 

julia> P[3]                                                        
6-element Array{Int64,1}:                                          
 8                                                                 
 9                                                                 
 0                                                                 
 0                                                                 
 5                                                                 
 6                                                                 

If you want to add a new element use push!():

julia> push!(P,[7,8,9])
4-element Array{Array{Int64,1},1}:
 [1, 2, 3, 4]
 [1, 4]
 [8, 9, 0, 0, 5, 6]
 [7, 8, 9]
Related