Uninitialized arrays in Julia

Let's say that I have an array of some composite type in Julia. I understand that I can’t just assign values ​​to an array, since its elements are undefined. For example, the code

type struct u::Int64 v::Int64 end X = Array(struct, 100) X[10].u = 3 

generates this error:

 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 

What is the standard way to handle this? At the moment, I'm just doing something like:

 samples = Array(Sample1d, num_samples) fill!(samples, Sample1d(0, 0, 0)) samples[i] = ... 

Is there a shorter or Julian way to do this?

+6
source share
2 answers

You can use fill to create and fill an array at the same time:

 type struct u::Int v::Int end struct() = struct(0, 0) X = fill(struct(), 100) X[10].u = 3 
+5
source

You can assign values ​​to uninitialized locations in arrays. You simply cannot retrieve values ​​from uninitialized locations.

+1
source

Source: https://habr.com/ru/post/971353/


All Articles