The function show_nd
in arrayshow.jl
starts with this:
function show_nd(io::IO, a::AbstractArray, print_matrix::Function, label_slices::Bool)
limit::Bool = get(io, :limit, false)
if isempty(a)
return
end
This function is called only for arrays that have at least 3 dimensions. I was wondering if it’s possible to have an empty 3D array? I do not know how to construct one.
Sure - Array{Int, 3}(undef, 2,3,4)
is a valid call which returns an uninitialized three dimensional array of Ints with size (2,3,4)
:
julia> Array{Int, 3}(undef, 2,3,4)
2×3×4 Array{Int64,3}:
[:, :, 1] =
36 10 35
12 24 16
[:, :, 2] =
34 24 33
22 28 31
[:, :, 3] =
32 31 139790621598032
34 35 139790621598032
[:, :, 4] =
139790621598032 139790622082656 139790621598032
139790621598032 139790622654688 139790552321744
This generalizes to N dimensions as well. There’s also zeros
, initializing each element with zero(T)
:
julia> zeros(Int, 2,3,4)
2×3×4 Array{Int64,3}:
[:, :, 1] =
0 0 0
0 0 0
[:, :, 2] =
0 0 0
0 0 0
[:, :, 3] =
0 0 0
0 0 0
[:, :, 4] =
0 0 0
0 0 0
I think here empty means “having 0 elements”. Eg
julia> a = Array{Int}(undef, 0, 0, 0)
0×0×0 Array{Int64,3}
julia> isempty(a)
true
3 Likes
Ah yes, that also makes sense - I was thinking empty as in no useful data
Ah of course, thanks very much! This extends to OffsetArray
s too where the axes ranges are empty.