假设我在 Julia 中有一个复合类型的数组。我知道我不能简单地将值分配到数组中,因为它的元素是未定义的。例如代码
type struct
u::Int64
v::Int64
end
X = Array(struct, 100)
X[10].u = 3
会产生这个错误:
ERROR: access to undefined reference
in getindex at array.jl:277
in include at boot.jl:238
in include_from_node1 at loading.jl:114
处理这个问题的标准方法是什么?现在我只是在做类似的事情:
samples = Array(Sample1d, num_samples)
fill!(samples, Sample1d(0, 0, 0))
samples[i] = ...
有没有更简洁或朱利安的方式来做到这一点?
最佳答案
您可以使用 fill
同时创建和填充数组:
type struct
u::Int
v::Int
end
struct() = struct(0, 0)
X = fill(struct(), 100)
X[10].u = 3
关于arrays - Julia 中的未初始化数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24416925/