# Unexpected behavior while creating an array of arrays

**URL:** <https://discourse.julialang.org/t/unexpected-behavior-while-creating-an-array-of-arrays/7503>\
**Category:** General Usage\
**Created:** [December 4, 2017, 6:15pm UTC](https://discourse.julialang.org/t/unexpected-behavior-while-creating-an-array-of-arrays/7503 "2017-12-04T18:15:50Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [December 4, 2017, 6:15pm UTC](https://discourse.julialang.org/t/unexpected-behavior-while-creating-an-array-of-arrays/7503/1 "2017-12-04T18:15:50Z")

</div>

Below I have a simplified example of a function that creates an array of arrays in a loop. A while back code similar to this tripped me up because I was unwittingly creating references in each component array. However, this is not the case with the code below. It does not create references and it does not store the unique values on each loop iteration.

```julia
function fun(Ntrials)
    space = linspace(0,.9,5)
    Nq = size(space,1)-1
    pred = fill(0.0,Nq)
    output = [fill(0.0,Nq) for t = 1:Ntrials]
    for trial = 1:Ntrials
        x = rand(100)
        for q = 1:Nq
            pred[q] = mean(x .<= space[q+1]) - mean(x .<= space[q])
        end
        println("pred ",pred)
        pred = @. log(max(pred,.10^10))
        output[trial] = pred
        println("output[1] ",output[1])
        println()
    end
    return output
end

x = fun(4)

#print out
pred [0.25, 0.23, 0.24, 0.22]
output[1] [-1.38629, -1.46968, -1.42712, -1.51413]

pred [0.17, 0.2, 0.26, 0.28]
output[1] [0.17, 0.2, 0.26, 0.28]

pred [0.3, 0.15, 0.25, 0.22]
output[1] [0.17, 0.2, 0.26, 0.28]

pred [0.19, 0.2, 0.26, 0.28]
output[1] [0.17, 0.2, 0.26, 0.28]

dump(x)
#print out

Array{Array{Float64,1}}((4,))
  1: Array{Float64}((4,)) [0.17, 0.2, 0.26, 0.28]
  2: Array{Float64}((4,)) [0.3, 0.15, 0.25, 0.22]
  3: Array{Float64}((4,)) [0.19, 0.2, 0.26, 0.28]
  4: Array{Float64}((4,)) [-1.66073, -1.60944, -1.34707, -1.27297]

```

However, if I remove `pred = @. log(max(pred,.10^10))`, it is true that `x[1] === x[2]`. I can’t imagine this is intended behavior. Does anyone know what is going on?

---

<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:** [December 4, 2017, 6:30pm UTC](https://discourse.julialang.org/t/unexpected-behavior-while-creating-an-array-of-arrays/7503/2 "2017-12-04T18:30:51Z")

</div>

You are putting the same `pred` into `output`.

---

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [December 4, 2017, 6:33pm UTC](https://discourse.julialang.org/t/unexpected-behavior-while-creating-an-array-of-arrays/7503/3 "2017-12-04T18:33:45Z")

</div>

> [@kristoffer.carlsson](#):
>
> You are putting the same pred into output.

Yes. However, I was expecting each array in output to have the last value of pred. Why does it not do that?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [December 4, 2017, 6:35pm UTC](https://discourse.julialang.org/t/unexpected-behavior-while-creating-an-array-of-arrays/7503/4 "2017-12-04T18:35:10Z")

</div>

If you remove the line in question, `pred` is always the same array (not the same content, because that changes after each iteration, but the same container).

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [December 4, 2017, 6:46pm UTC](https://discourse.julialang.org/t/unexpected-behavior-while-creating-an-array-of-arrays/7503/5 "2017-12-04T18:46:06Z")

</div>

See eg

> **[Values vs. Bindings: The Map is Not the Territory](https://www.johnmyleswhite.com/notebook/2014/09/06/values-vs-bindings-the-map-is-not-the-territory/)**
>
> Many newcomers to Julia are confused by the seemingly dissimilar behaviors of the following two functions:
> julia\> a = \[1, 2, 3\] 3-element Array{Int64,1}: 1 2 3 julia\> function foo!(a) a\[1\] = 10 return end foo! (generic function with 1 method)...

This should probably be a FAQ

---

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [December 4, 2017, 6:52pm UTC](https://discourse.julialang.org/t/unexpected-behavior-while-creating-an-array-of-arrays/7503/6 "2017-12-04T18:52:49Z")

</div>

Thanks for the link. I re-read it, but this particular case still does not make sense. Sorry if I am being dense. It just strikes me as odd that on the first loop it produces `output[1] [-1.38629, -1.46968, -1.42712, -1.51413]` and `output[1] [0.17, 0.2, 0.26, 0.28]` on every loop thereafter. I figured each array in output would be the same. Specifically, in my example I expected each array to be ` Array{Float64}((4,)) [-1.66073, -1.60944, -1.34707, -1.27297]`

When I set `output[trial] = @. log(max(pred,.10^10))`, that seems to produce a different arrays with the correct calculations, as expected.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [December 4, 2017, 6:57pm UTC](https://discourse.julialang.org/t/unexpected-behavior-while-creating-an-array-of-arrays/7503/7 "2017-12-04T18:57:16Z")

</div>

`pred` is just a name (binding). You are effectively making an array that looks like

```julia
[pred, pred, pred]

```

so each entry refers to the same object.  
(Disclaimer: I haven’t read the code in detail.)

---

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [December 4, 2017, 7:20pm UTC](https://discourse.julialang.org/t/unexpected-behavior-while-creating-an-array-of-arrays/7503/8 "2017-12-04T19:20:26Z")

</div>

Yes. What you have in your example makes sense. However, what I have in my example is something more like [pred1,pred2,…].

I think I figured out the issue: `pred = @. log(max(pred,.10^10))` creates a new binding of pred on each iteration. That prevents it from creating [pred,pred…]. Sorry for the noise!

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [December 4, 2017, 7:20pm UTC](https://discourse.julialang.org/t/unexpected-behavior-while-creating-an-array-of-arrays/7503/9 "2017-12-04T19:20:52Z")

</div>

Yes, exactly.
