Correct way to add elements to a multidimensional array?

numbers = Array{Int}(undef, 2,2)
println(typeof(numbers))

I attempted to use push!() but I kept getting an error. Is there a method specifically to add elements to multidimensional arrays?

1 Like

you can only extend it by another n-1 dim array (in the last axis usually). It’s not clear in general how to add just 1 element to a multi-dimensional array:

julia> nums = rand(2,2)
2×2 Array{Float64,2}:
 0.228904  0.405165
 0.954996  0.22869

julia> hcat(nums, [0,0])
2×3 Array{Float64,2}:
 0.228904  0.405165  0.0
 0.954996  0.22869   0.0

julia> vcat(nums, [0 0])
3×2 Array{Float64,2}:
 0.228904  0.405165
 0.954996  0.22869
 0.0       0.0
1 Like

It’s not clear in general how to add just 1 element to a multi-dimensional array

I think I may have misused the term element. If I had a 2x2 array, how would add another 2x2 element for example:

numbers = Array{Int}(undef, 2,2)
2×2 Array{Float64,2}:
1 2
3 4

If I wanted to add 5,6, I would use hcat(), correct?

julia> nums = [1 2;
               3 4]
2×2 Array{Int64,2}:
 1  2
 3  4
julia> vcat(nums, [5 6])
3×2 Array{Int64,2}:
 1  2
 3  4
 5  6

?

2 Likes
1 Like