Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays Julia中未初始化的数组_Arrays_Julia - Fatal编程技术网

Arrays Julia中未初始化的数组

Arrays Julia中未初始化的数组,arrays,julia,Arrays,Julia,假设我在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_node

假设我在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