Julia:向量的向量(数组的数组)

4

我正在尝试在Julia中创建一个由3D点构成的向量,这些点目前被表示为向量。然而,我无法弄清如何将这些向量放入一个向量中。以下是可以复现错误的最小示例:

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

在最后一行抛出了错误。
`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

有没有一种方法可以做到这一点,或者我错过了防止这种结构的东西?我应该使用矩阵吗?到目前为止我还没有使用矩阵,因为我想逐个迭代点,而不是批量转换它们。

2个回答

10

我觉得你在寻找

push!(bar, foo) 

3

append函数将第二个参数作为一个集合来处理,因此foo中每个元素都是一个Int,都无法被添加。你可以这样做:

append!(bar,[foo for i in 1:1])

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接