# Matrix: Need help to understand this behavior

**URL:** <https://www.ruby-forum.com/t/matrix-need-help-to-understand-this-behavior/146080>\
**Category:** Ruby\
**Created:** [August 17, 2008, 3:58pm UTC](https://www.ruby-forum.com/t/matrix-need-help-to-understand-this-behavior/146080 "2008-08-17T15:58:18Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![marcioab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f19dbf/32.png) [@marcioab](https://www.ruby-forum.com/u/marcioab)\
**Post date:** [August 17, 2008, 3:58pm UTC](https://www.ruby-forum.com/t/matrix-need-help-to-understand-this-behavior/146080/1 "2008-08-17T15:58:18Z")

</div>

a=[1]  
b=a # make matrix “b” equal matrix “a”, but expected 2  
separated matrices  
b[0]=2\*b[0] # I want to change matrix “b” (and not matrix “a”)  
p a # print matrix “a” and see the “issue”

The expected value in the matrix “a” should be 1, but instead it is 2.

Why ?

Thank you.  
Marcio

---

<div class="post-metadata">

**Author:** ![marcioab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f19dbf/32.png) [@marcioab](https://www.ruby-forum.com/u/marcioab)\
**Post date:** [August 17, 2008, 4:24pm UTC](https://www.ruby-forum.com/t/matrix-need-help-to-understand-this-behavior/146080/2 "2008-08-17T16:24:18Z")

</div>

On Sun, Aug 17, 2008 at 3:55 PM, Marcio B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Thank you.  
> Marcio

Typical Ruby behavior.

In Ruby, variables reference objects rather than contain values.  
Therefore, when you write b = a, b references the same object as a does.  
Therefore, when you modify b, you modify a.  
See the obejct\_ids in your code:

$\>cat mat.rb  
#!/usr/bin/env ruby -wKU

a = [1]  
b = a  
puts a.object\_id  
puts b.object\_id  
b[0] = 2 \* b[0]  
p a  
p b  
$\>ruby mat.rb  
284610  
284610  
[2]  
[2]  
$\>

See ? same objects.

To do what you want, you need to duplicate your array rather than just  
assign it :  
$\>cat dup\_mat.rb  
#!/usr/bin/env ruby -wKU

a = [1]  
b = a.dup  
puts a.object\_id  
puts b.object\_id  
b[0] = 2 \* b[0]  
p a  
p b  
$\>ruby dup\_mat.rb  
284540  
284530  
[1]  
[2]  
$\>

Hope it helped.

---

<div class="post-metadata">

**Author:** ![marcioab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f19dbf/32.png) [@marcioab](https://www.ruby-forum.com/u/marcioab)\
**Post date:** [August 17, 2008, 4:25pm UTC](https://www.ruby-forum.com/t/matrix-need-help-to-understand-this-behavior/146080/3 "2008-08-17T16:25:17Z")

</div>

Just to complement, as a contrast, the code below works as expected, or  
in other words, the content of matrix “a” is not changed when you change  
the content of matrix “b”.

b=  
a=[1]  
b[0]=a[0] # explicitly set a single element and not the entire  
matrix  
b[0]=2\*b[0]  
p a

Marcio

Marcio B. wrote:

> a=[1]  
> b=a # make matrix “b” equal matrix “a”, but expected 2  
> separated matrices  
> b[0]=2\*b[0] # I want to change matrix “b” (and not matrix “a”)  
> p a # print matrix “a” and see the “issue”
> 
> The expected value in the matrix “a” should be 1, but instead it is 2.
> 
> Why ?
> 
> Thank you.  
> Marcio

---

<div class="post-metadata">

**Author:** ![marcioab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f19dbf/32.png) [@marcioab](https://www.ruby-forum.com/u/marcioab)\
**Post date:** [August 17, 2008, 4:29pm UTC](https://www.ruby-forum.com/t/matrix-need-help-to-understand-this-behavior/146080/4 "2008-08-17T16:29:37Z")

</div>

Thank you Zacek. I understand your point and that help very much.

Have a nice day !!  
Marcio

Frantisek ZACEK wrote:

> On Sun, Aug 17, 2008 at 3:55 PM, Marcio B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > Thank you.  
> > Marcio
> 
> Typical Ruby behavior.
> 
> …
> 
> Hope it helped.

---

<div class="post-metadata">

**Author:** ![marcioab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f19dbf/32.png) [@marcioab](https://www.ruby-forum.com/u/marcioab)\
**Post date:** [August 17, 2008, 4:57pm UTC](https://www.ruby-forum.com/t/matrix-need-help-to-understand-this-behavior/146080/5 "2008-08-17T16:57:05Z")

</div>

Zacek and all: how to duplicate a matrix that has elements that are also  
matrix ?  
For example:

a = [[1]]  
b = a.dup  
puts a.object\_id  
puts b.object\_id  
puts a[0].object\_id # the same object  
puts b[0].object\_id # the same object  
b[0][0] = 2 \* b[0][0]  
p a  
p b

So, matrix “a” and “b” are different objects but each internal matrix  
element continue to be the same object. Do I need to duplicate each one  
?

Thank you  
Marcio

---

<div class="post-metadata">

**Author:** ![marcioab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f19dbf/32.png) [@marcioab](https://www.ruby-forum.com/u/marcioab)\
**Post date:** [August 17, 2008, 4:30pm UTC](https://www.ruby-forum.com/t/matrix-need-help-to-understand-this-behavior/146080/6 "2008-08-17T16:30:09Z")

</div>

On Sun, Aug 17, 2008 at 4:22 PM, Marcio B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Marcio

Here you create explicitely two objects therefore : no problem whereas  
on the first snippet, you only have one.

---

<div class="post-metadata">

**Author:** ![marcioab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f19dbf/32.png) [@marcioab](https://www.ruby-forum.com/u/marcioab)\
**Post date:** [August 17, 2008, 11:59pm UTC](https://www.ruby-forum.com/t/matrix-need-help-to-understand-this-behavior/146080/7 "2008-08-17T23:59:33Z")

</div>

On Sun, Aug 17, 2008 at 4:53 PM, Marcio B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> b[0][0] = 2 \* b[0][0]  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

I don’t think there is a simple way to achieve that.  
I would suggest:  
a=[[1]]  
b=a.dup.colllect { |e| e.dup }

---

<div class="post-metadata">

**Author:** ![marcioab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f19dbf/32.png) [@marcioab](https://www.ruby-forum.com/u/marcioab)\
**Post date:** [August 19, 2008, 12:37am UTC](https://www.ruby-forum.com/t/matrix-need-help-to-understand-this-behavior/146080/8 "2008-08-19T00:37:20Z")

</div>

> I don’t think there is a simple way to achieve that.  
> I would suggest:  
> a=[[1]]  
> b=a.dup.colllect { |e| e.dup }

Ok.  
So I need to create “my own” function to do that (just in case  
a=[[[1]]])

Thank you

---

<div class="post-metadata">

**Author:** ![marcioab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f19dbf/32.png) [@marcioab](https://www.ruby-forum.com/u/marcioab)\
**Post date:** [August 18, 2008, 1:27am UTC](https://www.ruby-forum.com/t/matrix-need-help-to-understand-this-behavior/146080/9 "2008-08-18T01:27:18Z")

</div>

On Sun, Aug 17, 2008 at 9:53 AM, Marcio B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> b[0][0] = 2 \* b[0][0]  
> p a  
> p b
> 
> So, matrix “a” and “b” are different objects but each internal matrix  
> element continue to be the same object. Do I need to duplicate each one  
> ?

It’s a Matrix, right? (some of this stolen from a previous thread)…

require ‘matrix’  
class Matrix  
def =(i, j, n)  
@rows[i][j] = n  
end  
end

m = Matrix[[‘a’, ‘b’], [‘c’, ‘d’]]  
k = m.clone  
k[0, 0] = ‘e’  
puts k.to\_a  
puts m.to\_a

Todd

---

<div class="post-metadata">

**Author:** ![marcioab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f19dbf/32.png) [@marcioab](https://www.ruby-forum.com/u/marcioab)\
**Post date:** [August 19, 2008, 12:42am UTC](https://www.ruby-forum.com/t/matrix-need-help-to-understand-this-behavior/146080/10 "2008-08-19T00:42:01Z")

</div>

> It’s a Matrix, right? (some of this stolen from a previous thread)…
> 
> require ‘matrix’  
> class Matrix  
> def =(i, j, n)  
> @rows[i][j] = n  
> end  
> end
> 
> m = Matrix[[‘a’, ‘b’], [‘c’, ‘d’]]  
> k = m.clone  
> k[0, 0] = ‘e’  
> puts k.to\_a  
> puts m.to\_a
> 
> Todd

At first I was not considering to handle them using Matrix class (just  
Array class) but thank you to call attention to this clone method of  
Matrix.

Marcio
