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

**URL:** <https://discourse.julialang.org/t/stucked-in-2-dimensions-failed-to-create-a-pre-filled-array-in-3-dimensions/16082>\
**Category:** New to Julia\
**Created:** [October 9, 2018, 4:48pm UTC](https://discourse.julialang.org/t/stucked-in-2-dimensions-failed-to-create-a-pre-filled-array-in-3-dimensions/16082 "2018-10-09T16:48:40Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ralf\_Dietrich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ralf_dietrich/32/5643_2.png) [@Ralf\_Dietrich](https://discourse.julialang.org/u/Ralf_Dietrich)\
**Post date:** [October 9, 2018, 4:48pm UTC](https://discourse.julialang.org/t/stucked-in-2-dimensions-failed-to-create-a-pre-filled-array-in-3-dimensions/16082/1 "2018-10-09T16:48:40Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [October 9, 2018, 4:59pm UTC](https://discourse.julialang.org/t/stucked-in-2-dimensions-failed-to-create-a-pre-filled-array-in-3-dimensions/16082/2 "2018-10-09T16:59:28Z")

</div>

```julia
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

```

---

<div class="post-metadata">

**Author:** ![Liso](https://avatars.discourse-cdn.com/v4/letter/l/898d66/32.png) [@Liso](https://discourse.julialang.org/u/Liso)\
**Post date:** [October 9, 2018, 5:12pm UTC](https://discourse.julialang.org/t/stucked-in-2-dimensions-failed-to-create-a-pre-filled-array-in-3-dimensions/16082/3 "2018-10-09T17:12:04Z")

</div>

```julia
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

```

---

<div class="post-metadata">

**Author:** ![Ralf\_Dietrich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ralf_dietrich/32/5643_2.png) [@Ralf\_Dietrich](https://discourse.julialang.org/u/Ralf_Dietrich)\
**Post date:** [October 9, 2018, 9:14pm UTC](https://discourse.julialang.org/t/stucked-in-2-dimensions-failed-to-create-a-pre-filled-array-in-3-dimensions/16082/4 "2018-10-09T21:14:37Z")

</div>

> [@Liso](#):
>
> a = cat([1 2 3; 4 5 6; 7 8 9] , [11 12 13; 14 15 16; 17 18 19];dims=3)

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?

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [October 9, 2018, 9:37pm UTC](https://discourse.julialang.org/t/stucked-in-2-dimensions-failed-to-create-a-pre-filled-array-in-3-dimensions/16082/5 "2018-10-09T21:37:37Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [October 10, 2018, 12:45am UTC](https://discourse.julialang.org/t/stucked-in-2-dimensions-failed-to-create-a-pre-filled-array-in-3-dimensions/16082/6 "2018-10-10T00:45:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Nosferican](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nosferican/32/9275_2.png) [@Nosferican](https://discourse.julialang.org/u/Nosferican)\
**Post date:** [October 10, 2018, 1:41am UTC](https://discourse.julialang.org/t/stucked-in-2-dimensions-failed-to-create-a-pre-filled-array-in-3-dimensions/16082/7 "2018-10-10T01:41:04Z")

</div>

How about,

```julia
a = zeros(i, j, k)

```

or

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

```

---

<div class="post-metadata">

**Author:** ![Ralf\_Dietrich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ralf_dietrich/32/5643_2.png) [@Ralf\_Dietrich](https://discourse.julialang.org/u/Ralf_Dietrich)\
**Post date:** [October 10, 2018, 8:01am UTC](https://discourse.julialang.org/t/stucked-in-2-dimensions-failed-to-create-a-pre-filled-array-in-3-dimensions/16082/8 "2018-10-10T08:01:58Z")

</div>

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](https://global.discourse-cdn.com/julialang/original/3X/c/8/c81f347965c53e832ef20128ab70b237ec1bd52d.png)

---

<div class="post-metadata">

**Author:** ![Ralf\_Dietrich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ralf_dietrich/32/5643_2.png) [@Ralf\_Dietrich](https://discourse.julialang.org/u/Ralf_Dietrich)\
**Post date:** [October 10, 2018, 8:11am UTC](https://discourse.julialang.org/t/stucked-in-2-dimensions-failed-to-create-a-pre-filled-array-in-3-dimensions/16082/9 "2018-10-10T08:11:57Z")

</div>

Thank you for all your help!
