在朱莉娅推动矢量矢量

时间:2017-08-16 23:30:55

标签: vector julia

这是使用Julia 0.6

说我有一个Ints矢量载体

x = Vector{Int64}[] resize!(x, 10) fill!(x, [0])

我想推送到外部矢量中的选定矢量,但无法使其工作。

push!(x[1], 2)

julia> x
10-element Array{Array{Int64,1},1}:  
 [0, 2]  
 [0, 2]  
 [0, 2]  
 [0, 2]  
 [0, 2]  
 [0, 2]  
 [0, 2]  
 [0, 2]  
 [0, 2]  
 [0, 2]  

最终将对推到每个向量。我是否以错误的方式思考这个问题?有没有办法推送到一个选定的Vector?

使用其中一个向量的视图没有帮助。

a = x[1]
push!(a, 3)

julia> x
10-element Array{Array{Int64,1},1}:
 [0, 2, 3]
 [0, 2, 3]
 [0, 2, 3]
 [0, 2, 3]
 [0, 2, 3]
 [0, 2, 3]
 [0, 2, 3]
 [0, 2, 3]
 [0, 2, 3]
 [0, 2, 3]

是否可以这样做?

1 个答案:

答案 0 :(得分:-1)

`deepcopy'应该有助于获得单独的对象/数组。然而,令人惊讶的是它不起作用?!

julia> x = Vector{Int64}[];
julia> resize!(x, 10);
julia> fill!(x, [0])
10-element Array{Array{Int64,1},1}:
 [0]
 [0]
 [0]
 [0]
 [0]
 [0]
 [0]
 [0]
 [0]
 [0]

julia> x = deepcopy(x)
julia> push!(x[1], 2)
    10-element Array{Array{Int64,1},1}:
 [0, 2]
 [0, 2]
 [0, 2]
 [0, 2]
 [0, 2]
 [0, 2]
 [0, 2]
 [0, 2]
 [0, 2]
 [0, 2]

不是我期待的