# Changing an element inside array of arrays

**URL:** <https://discourse.julialang.org/t/changing-an-element-inside-array-of-arrays/29829>\
**Category:** New to Julia\
**Created:** [October 12, 2019, 6:44pm UTC](https://discourse.julialang.org/t/changing-an-element-inside-array-of-arrays/29829 "2019-10-12T18:44:55Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vadim\_Zotin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vadim_zotin/32/10774_2.png) [@Vadim\_Zotin](https://discourse.julialang.org/u/Vadim_Zotin)\
**Post date:** [October 12, 2019, 6:44pm UTC](https://discourse.julialang.org/t/changing-an-element-inside-array-of-arrays/29829/1 "2019-10-12T18:44:55Z")

</div>

Hello!  
Can anyone explain a strange result of changing one element inside an array of arrays?

julia\> b1=fill(zeros(4),4)  
4-element Array{Array{Float64,1},1}:  
[0.0, 0.0, 0.0, 0.0]  
[0.0, 0.0, 0.0, 0.0]  
[0.0, 0.0, 0.0, 0.0]  
[0.0, 0.0, 0.0, 0.0]

julia\> b1[1][1]=100.0  
100.0

julia\> b1  
4-element Array{Array{Float64,1},1}:  
[100.0, 0.0, 0.0, 0.0]  
[100.0, 0.0, 0.0, 0.0]  
[100.0, 0.0, 0.0, 0.0]  
[100.0, 0.0, 0.0, 0.0]

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [October 12, 2019, 7:22pm UTC](https://discourse.julialang.org/t/changing-an-element-inside-array-of-arrays/29829/2 "2019-10-12T19:22:45Z")

</div>

What you actually do is:

```julia
julia> z=zeros(4)
4-element Array{Float64,1}:
 0.0
 0.0
 0.0
 0.0

julia> b1=[z,z,z,z]
4-element Array{Array{Float64,1},1}:
 [0.0, 0.0, 0.0, 0.0]
 [0.0, 0.0, 0.0, 0.0]
 [0.0, 0.0, 0.0, 0.0]
 [0.0, 0.0, 0.0, 0.0]

```

Now doing

```julia
julia> z[1]=100.0
100.0

julia> b1
4-element Array{Array{Float64,1},1}:
 [100.0, 0.0, 0.0, 0.0]
 [100.0, 0.0, 0.0, 0.0]
 [100.0, 0.0, 0.0, 0.0]
 [100.0, 0.0, 0.0, 0.0]

```

Is not so surprising anymore.

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [October 12, 2019, 7:25pm UTC](https://discourse.julialang.org/t/changing-an-element-inside-array-of-arrays/29829/3 "2019-10-12T19:25:27Z")

</div>

What you want, I would create with:

```julia
julia> b2 = [zeros(4) for i in 1:4]
4-element Array{Array{Float64,1},1}:
 [0.0, 0.0, 0.0, 0.0]
 [0.0, 0.0, 0.0, 0.0]
 [0.0, 0.0, 0.0, 0.0]
 [0.0, 0.0, 0.0, 0.0]

julia> b2[1][1]=100.0
100.0

julia> b2
4-element Array{Array{Float64,1},1}:
 [100.0, 0.0, 0.0, 0.0]
 [0.0, 0.0, 0.0, 0.0]
 [0.0, 0.0, 0.0, 0.0]
 [0.0, 0.0, 0.0, 0.0]

```

but there are probably more elegant ways.

---

<div class="post-metadata">

**Author:** ![Vadim\_Zotin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vadim_zotin/32/10774_2.png) [@Vadim\_Zotin](https://discourse.julialang.org/u/Vadim_Zotin)\
**Post date:** [October 12, 2019, 8:20pm UTC](https://discourse.julialang.org/t/changing-an-element-inside-array-of-arrays/29829/4 "2019-10-12T20:20:41Z")

</div>

Thank you for the decision.  
Yet it’s still unclear how the equal arrays can produce different results.

julia\> b1=fill(zeros(4),4)  
4-element Array{Array{Float64,1},1}:  
[0.0, 0.0, 0.0, 0.0]  
[0.0, 0.0, 0.0, 0.0]  
[0.0, 0.0, 0.0, 0.0]  
[0.0, 0.0, 0.0, 0.0]

julia\> b2=[zeros(4) for i in 1:4]  
4-element Array{Array{Float64,1},1}:  
[0.0, 0.0, 0.0, 0.0]  
[0.0, 0.0, 0.0, 0.0]  
[0.0, 0.0, 0.0, 0.0]  
[0.0, 0.0, 0.0, 0.0]

julia\> isequal(b1,b2)  
true

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [October 12, 2019, 8:28pm UTC](https://discourse.julialang.org/t/changing-an-element-inside-array-of-arrays/29829/5 "2019-10-12T20:28:19Z")

</div>

```julia
julia> b1==b2
true

julia> b1===b2
false

```

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [October 12, 2019, 8:30pm UTC](https://discourse.julialang.org/t/changing-an-element-inside-array-of-arrays/29829/6 "2019-10-12T20:30:17Z")

</div>

`isequal` (or also `==` FWIW) only compares the value. It ignores many other properties of the object being compared.

As examples.

```julia
julia> a = [1, 2, 3]; b = 1:3
1:3

julia> isequal(a, b)
true

julia> push!(a, 1)
4-element Array{Int64,1}:
 1
 2
 3
 1

julia> push!(b, 1)
ERROR: MethodError: no method matching resize!(::UnitRange{Int64}, ::Int64)
Closest candidates are:
  resize!(::Array{T,1} where T, ::Integer) at array.jl:1016
  resize!(::BitArray{1}, ::Integer) at bitarray.jl:773
Stacktrace:
 [1] _append!(::UnitRange{Int64}, ::Base.HasLength, ::Tuple{Int64}) at ./array.jl:920
 [2] append!(::UnitRange{Int64}, ::Tuple{Int64}) at ./array.jl:914
 [3] push!(::UnitRange{Int64}, ::Int64) at ./array.jl:915
 [4] top-level scope at REPL[4]:1

julia> isequal(0x1, 1)
true

julia> -0x1
0xff

julia> -1
-1

```

OTOH, `is` (or `===`) checks if the two objects are the same one. There is not a general way to compare if the two objects “behaves identically” since that’s not really a well defined concept anyway…

---

<div class="post-metadata">

**Author:** ![zgornel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zgornel/32/217487_2.png) [@zgornel](https://discourse.julialang.org/u/zgornel)\
**Post date:** [October 12, 2019, 8:44pm UTC](https://discourse.julialang.org/t/changing-an-element-inside-array-of-arrays/29829/7 "2019-10-12T20:44:45Z")

</div>

In the `fill` case there is only one array referenced 4 times. Equality relates to the values in the object, not the memory layout.

---

<div class="post-metadata">

**Author:** ![Vadim\_Zotin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vadim_zotin/32/10774_2.png) [@Vadim\_Zotin](https://discourse.julialang.org/u/Vadim_Zotin)\
**Post date:** [October 12, 2019, 9:04pm UTC](https://discourse.julialang.org/t/changing-an-element-inside-array-of-arrays/29829/8 "2019-10-12T21:04:02Z")

</div>

Thanks a lot for the explanations.  
To check it I can compare elements inside both arrays.

julia\> b1[1]===b1[2]  
true

julia\> b2[1]===b2[2]  
false
