# Weird behavior with 3-dimensional arrays (beginner)

**URL:** <https://discourse.julialang.org/t/weird-behavior-with-3-dimensional-arrays-beginner/55119>\
**Category:** New to Julia\
**Created:** [February 12, 2021, 4:06am UTC](https://discourse.julialang.org/t/weird-behavior-with-3-dimensional-arrays-beginner/55119 "2021-02-12T04:06:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![samerb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/samerb/32/9242_2.png) [@samerb](https://discourse.julialang.org/u/samerb)\
**Post date:** [February 12, 2021, 4:06am UTC](https://discourse.julialang.org/t/weird-behavior-with-3-dimensional-arrays-beginner/55119/1 "2021-02-12T04:06:36Z")

</div>

I am trying to work with 3-dimensions arrays. My understanding is that they are just like matrices but with one extra index (so like a “deck” of matrices).

Sometimes the arrays appear to be behaving as expected but sometimes not. Here is an example where I got completely unexpected results. (It seems like maybe sometimes running the same code multiple times is giving me different results possibly due to how the arrays are stored in memory? I am working in Jupyterlab)

Here is some code I ran which gave me the results in the screenshot below.

I thought when I initialize the arrays, they would just be zero, and then scalar multiplication multiplies each component and array addition is component-wise. That should give a 4 in position [1,1,1], a 4 in position [2,1,1] and zeros elsewhere.

What am I missing? Thanks

```julia
x = Array{Float64}(undef, (2,5,7))
# adjust

y = Array{Float64}(undef, (2,5,7))

x[2,1,1] = 7
y[1,1,1] = 4
x
y
2*x + y

```

 ![Screen Shot 2021-02-11 at 9.44.00 PM](https://global.discourse-cdn.com/julialang/original/3X/f/9/f9b3b46e60909fe91f1a30f7b2ca4e3faa8b3c3c.png)

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [February 12, 2021, 4:32am UTC](https://discourse.julialang.org/t/weird-behavior-with-3-dimensional-arrays-beginner/55119/2 "2021-02-12T04:32:49Z")

</div>

`undef` means `uninitialized`

You want the function `zeros` to initialize an array of zeros.

---

<div class="post-metadata">

**Author:** ![briochemc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/briochemc/32/4209_2.png) [@briochemc](https://discourse.julialang.org/u/briochemc)\
**Post date:** [February 12, 2021, 4:46am UTC](https://discourse.julialang.org/t/weird-behavior-with-3-dimensional-arrays-beginner/55119/3 "2021-02-12T04:46:46Z")

</div>

> [@samerb](#):
>
> ```julia
> x = Array{Float64}(undef, (2,5,7))
> # adjust
> 
> y = Array{Float64}(undef, (2,5,7))
> 
> ```

To complement this answer, you should change the code above into

```julia
x = zeros(2,5,7)
# adjust

y = zeros(2,5,7)

```
