What is the best way in Julia to check whether an array entry is #undef?
Example:
julia> a = Array(Vector,2)
julia> isdefined(a[1]) # fails
julia> isempty(a[1]) # fails
undef is not itself an undefined value, but rather a special array initializer that flags it can initialize with undefined values. Or, another way of putting this: Julia (unlike, say, Javascript) does not have a defined undefined value that you can compare undefined things against.
In Julia you can use the isempty() function documented here. Note that I included the length check as well in case that will help your use case. Show activity on this post. For arrays one can also simply use a == [] .
The easiest way to initialize them are probably list comprehensions, for example: julia> [[Vector{Float64}(undef, 4) for _ = 1:5] for _ = 1:6] 5-element Array{Array{Array{Float64,1},1},1}: ...
A 1D array can be created by simply writing array elements within square brackets separated by commas(, ) or semicolon(;). A 2D array can be created by writing a set of elements without commas and then separating that set with another set of values by a semicolon.
You can push the access into isdefined
by using isdefined(a, 1)
instead of isdefined(a[1])
:
julia> a = Array(Vector,2);
julia> a[2] = {10}
1-element Array{Any,1}:
10
julia> a
2-element Array{Array{T,1},1}:
#undef
{10}
julia> isdefined(a[1])
ERROR: access to undefined reference
in getindex at array.jl:246
julia> isdefined(a, 1)
false
julia> isdefined(a, 2)
true
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With