# Matrix values

**URL:** <https://discourse.julialang.org/t/matrix-values/47925>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [October 7, 2020, 12:00pm UTC](https://discourse.julialang.org/t/matrix-values/47925 "2020-10-07T12:00:46Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![ricardomribeiro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ricardomribeiro/32/18493_2.png) [@ricardomribeiro](https://discourse.julialang.org/u/ricardomribeiro)\
**Post date:** [October 7, 2020, 12:00pm UTC](https://discourse.julialang.org/t/matrix-values/47925/1 "2020-10-07T12:00:46Z")

</div>

I am working with a code that first defines a matrix and then changes some values of that matrix.

For example:  
temp1 = zeros(5,5);  
temp2 = temp1;  
temp2[[1; 3]] = [4; 3];

I would expect that the values of matrix temp1 would remain unchanged. But somehow the values of matrix temp1 are the same as the values of matrix temp2 at the end of the code.

temp1  
5×5 Array{Float64,2}:  
4.0 0.0 0.0 0.0 0.0  
0.0 0.0 0.0 0.0 0.0  
3.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

temp2  
5×5 Array{Float64,2}:  
4.0 0.0 0.0 0.0 0.0  
0.0 0.0 0.0 0.0 0.0  
3.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

This behavior seems odd to me. Why is Julia doing this? Can somebody help me with this point?

Many thanks in advance.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [October 7, 2020, 12:08pm UTC](https://discourse.julialang.org/t/matrix-values/47925/2 "2020-10-07T12:08:32Z")

</div>

If you don’t want this behavior, `temp2.=temp1` will make a copy.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [October 7, 2020, 12:10pm UTC](https://discourse.julialang.org/t/matrix-values/47925/3 "2020-10-07T12:10:21Z")

</div>

As Oscar says, `temp2 = temp1` just creates a new variable `temp2` that is a different name for `temp1`, not a new underlying object. Consider:

```julia
julia> t1 = zeros(5, 5);

julia> t2 = t1;

julia> t2 === t1
true

help?> ===
search: === == !==

  ===(x,y) -> Bool
  ≡(x,y) -> Bool

  Determine whether x and y are identical, in the sense that no program could distinguish them. First the types of x and y are compared. If those are identical, mutable objects are compared by address in
  memory and immutable objects (such as numbers) are compared by contents at the bit level. This function is sometimes called "egal". It always returns a Bool value.

  Examples
  ≡≡≡≡≡≡≡≡≡≡

  julia> a = [1, 2]; b = [1, 2];
  
  julia> a == b
  true
  
  julia> a === b
  false
  
  julia> a === a
  true

```

Here’s a helpful blogpost that explains some of this: [Values vs. Bindings: The Map is Not the Territory | juliabloggers.com](https://www.juliabloggers.com/values-vs-bindings-the-map-is-not-the-territory-3/)

---

<div class="post-metadata">

**Author:** ![ricardomribeiro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ricardomribeiro/32/18493_2.png) [@ricardomribeiro](https://discourse.julialang.org/u/ricardomribeiro)\
**Post date:** [October 7, 2020, 12:11pm UTC](https://discourse.julialang.org/t/matrix-values/47925/4 "2020-10-07T12:11:50Z")

</div>

Many thanks, Oscar! temp2.=temp1 gave me an error, but temp2=copy(temp1) worked.

---

<div class="post-metadata">

**Author:** ![ricardomribeiro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ricardomribeiro/32/18493_2.png) [@ricardomribeiro](https://discourse.julialang.org/u/ricardomribeiro)\
**Post date:** [October 7, 2020, 12:14pm UTC](https://discourse.julialang.org/t/matrix-values/47925/5 "2020-10-07T12:14:40Z")

</div>

Understood perfectly now. Many thanks! I have to go over the code from the beginning to see if there are more instances like this. This reasoning is different from Matlab and it was unexpected to me. Thank you once again.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [October 7, 2020, 12:17pm UTC](https://discourse.julialang.org/t/matrix-values/47925/6 "2020-10-07T12:17:17Z")

</div>

If you’re coming from MATLAB it might be useful to look at: [Noteworthy Differences from other Languages · The Julia Language](https://docs.julialang.org/en/v1/manual/noteworthy-differences/)

Incidentally, the topic of this thread is point 2 on the list 🙂

---

<div class="post-metadata">

**Author:** ![ricardomribeiro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ricardomribeiro/32/18493_2.png) [@ricardomribeiro](https://discourse.julialang.org/u/ricardomribeiro)\
**Post date:** [October 7, 2020, 12:34pm UTC](https://discourse.julialang.org/t/matrix-values/47925/7 "2020-10-07T12:34:50Z")

</div>

Many, many thanks, Nils!! It will be a great help. Thank you once again.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [October 7, 2020, 1:32pm UTC](https://discourse.julialang.org/t/matrix-values/47925/8 "2020-10-07T13:32:18Z")

</div>

I am late, but I will shamelessly promote [my post trying to build a simple mental model of why you should see Julia “variables” as labels, not boxes](https://discourse.julialang.org/t/is-it-appropriate-to-say-a-variable-is-a-label-or-box/46068/19).

---

<div class="post-metadata">

**Author:** ![ricardomribeiro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ricardomribeiro/32/18493_2.png) [@ricardomribeiro](https://discourse.julialang.org/u/ricardomribeiro)\
**Post date:** [October 7, 2020, 3:27pm UTC](https://discourse.julialang.org/t/matrix-values/47925/9 "2020-10-07T15:27:01Z")

</div>

Many thanks, Henrique! Vem muito a tempo 🙂

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [October 7, 2020, 5:43pm UTC](https://discourse.julialang.org/t/matrix-values/47925/10 "2020-10-07T17:43:17Z")

</div>

XD, bom saber.
