Stucked in 2 Dimensions (Failed to create a pre-filled Array in 3 Dimensions)

I tried to create a prefilled Array in 3 dimension and failed (see no.3):

no. 1:
a = [1,2,3] # -> 1 Dimensional Matrix
Output:
3-element Array{Int64,1}:
1
2
3

no. 2:
a = [1 2 3; 4 5 6; 7 8 9] # -> 2 Dimensional Matrix
Output
3×3 Array{Int64,2}:
1 2 3
4 5 6
7 8 9

no. 3:
a = ??? # -> 3 Dimensional Matrix?

Any ideas?

julia> f(i,j,k) = i+j+k
f (generic function with 2 methods)

julia> [f(i,j,k) for i in 1:2, j in 1:2, k in 1:2]
2×2×2 Array{Int64,3}:
[:, :, 1] =
 3  4
 4  5

[:, :, 2] =
 4  5
 5  6
2 Likes
julia> a = cat([1  2  3;  4  5  6;  7  8  9] , 
               [11 12 13; 14 15 16; 17 18 19];dims=3)
3×3×2 Array{Int64,3}:
[:, :, 1] =
 1  2  3
 4  5  6
 7  8  9

[:, :, 2] =
 11  12  13
 14  15  16
 17  18  19
2 Likes

The last idea is pretty close to that what I’m looking for. Is there any chance to realize it without “for” or “cat” just jusing “”,;(){}’’ or something similar?

No, I don’t think so. The various cominations of [], ;, and space are just syntactic sugars that get lowered into hcat, vcat, or hvcat, all of which operate only along the first or second dimension. They’re not any more efficient than calling cat yourself, just easier to type. I’d suggest wrapping up whatever construction you want to do in a function (using one of the solutions above), and then just calling that function.

1 Like

You can also first make a vector and then reshape it into a 3D array using the reshape function.

2 Likes

How about,

a = zeros(i, j, k)

or

a = Array{T}(undef, i, j, k)

Tanks to your hint I found a solution that works pretty good for me, because you just need to edit the (3, 3) to increase/decrease the dimension (less work):

image

Thank you for all your help!