Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Julia: Vector of Vector (Array of Arrays)

Tags:

julia

I'm trying to make a vector of 3D points in Julia, which I currently have as vectors themselves. However, I cannot figure out how to get these vectors into a vector. My minimal example to reproduce the error is:

foo = rand(3) #Vector Float64, 3
bar = Vector{Float64}[] #Vector Array{Float64,1} 0
append!(bar,foo) #Throws an error

Which throws the error at the last line

`convert` has no method matching convert(::Type{Array{Float64,1}}, ::Float64)
in copy! at abstractarray.jl:197
in append! at array.jl:478
in include_string at loading.jl:97
in include_string at C:\Users\Alex\.julia\v0.3\Jewel\src\eval.jl:36
in anonymous at C:\Users\Alex\.julia\v0.3\Jewel\src\LightTable\eval.jl:68
in handlecmd at C:\Users\Alex\.julia\v0.3\Jewel\src\LightTable/LightTable.jl:65
in handlenext at C:\Users\Alex\.julia\v0.3\Jewel\src\LightTable/LightTable.jl:81
in server at C:\Users\Alex\.julia\v0.3\Jewel\src\LightTable/LightTable.jl:22
in server at C:\Users\Alex\.julia\v0.3\Jewel\src\Jewel.jl:18
in include at boot.jl:245
in include_from_node1 at loading.jl:128
in process_options at client.jl:285
in _start at client.jl:354

Is there a way to do this, or am I missing something that prevents such a structure? Should I be using matrices instead? I haven't so far since I want to iterate over the points, not transform them in bulk.

like image 320
sabreitweiser Avatar asked Dec 11 '22 20:12

sabreitweiser


2 Answers

I think you're looking for

push!(bar, foo) 
like image 58
David P. Sanders Avatar answered Dec 24 '22 19:12

David P. Sanders


append takes the second paramater as a collection, so each element of foo (each one an Int) will fail to be added. You can do:

append!(bar,[foo for i in 1:1])
like image 33
Felipe Lema Avatar answered Dec 24 '22 17:12

Felipe Lema