Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Julia: check whether array entry is undef

Tags:

julia

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
like image 497
Mageek Avatar asked Jul 29 '14 16:07

Mageek


People also ask

Is undef a Julia?

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.

How do I check if an array is empty Julia?

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 == [] .

How do you initialize a vector in Julia?

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}: ...

How do you create an array in Julia?

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.


1 Answers

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
like image 172
DSM Avatar answered Sep 18 '22 14:09

DSM