# How do I define a dynamic Matrix or

**URL:** <https://www.ruby-forum.com/t/how-do-i-define-a-dynamic-matrix-or/118562>\
**Category:** Ruby\
**Created:** [October 28, 2007, 10:59pm UTC](https://www.ruby-forum.com/t/how-do-i-define-a-dynamic-matrix-or/118562 "2007-10-28T22:59:30Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ribes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f4b2a3/32.png) [@ribes](https://www.ruby-forum.com/u/ribes)\
**Post date:** [October 28, 2007, 10:59pm UTC](https://www.ruby-forum.com/t/how-do-i-define-a-dynamic-matrix-or/118562/1 "2007-10-28T22:59:30Z")

</div>

Hello Team,

On my quest to learn Ruby, I implemented a known algorithm to solve an  
odd  
nXn magic square.  
I tested my code using a 3x3 and a 5x5 magic squares.  
The problem that I am having is that I hard-code the initialization  
matrix.  
In other words, if it is a 3x3 I define:

mm = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]

For a 5 x 5 I define :

mm = [[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]]

I want to be able to solve any size odd magic square. To do so, I need  
to be  
able to define my matrix dynamically and on demand.  
Can anyone tell me how to do that?

Attached is my current code. Please feel free to criticize it and make  
recommendations. My disclaimer: I am a Ruby apprentice.

Thank you

Victor

#!/usr/local/bin/ruby -d -W0

def mag(size)  
r = 0 # row  
c = 0 # column  
index = 0 # pointer

# mm = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]

```
mm = [[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]]

```
maxLoop = size * size # Magic Square size

1.upto(maxLoop) do |n|

  case
      when n == 1
          c = (size / 2).floor # Will keep the

```

integer part, giving us the center in row 0  
mm[c][r] = n # This is:  
mm[c][0]  
when index \>= size  
index = 0  
r = r + 1  
mm[c][r] = n  
puts " "  
else  
c = c + 1  
if c \>= size  
c = 0  
end

```
          r = r -1
          if r < 0
            r = size - 1
          end
          mm[c][r] = n
      end
      index = index + 1

  puts "[#{c}][#{r}] = #{mm[c][r]} "

```

end # End upto

```
##############
# Output the matrix #
##############
puts
0.upto(size -1) do |r|
  0.upto(size -1) do |c|
    print "#{mm[c][r]} "
  end
  puts " "
end

```

end # End method mag

# We start here

p1 = ARGV[0].to\_i

mag( p1 )

---

<div class="post-metadata">

**Author:** ![ribes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f4b2a3/32.png) [@ribes](https://www.ruby-forum.com/u/ribes)\
**Post date:** [October 28, 2007, 11:26pm UTC](https://www.ruby-forum.com/t/how-do-i-define-a-dynamic-matrix-or/118562/2 "2007-10-28T23:26:58Z")

</div>

Hi –

On 28/10/2007, Victor R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> For a 5 x 5 I define :  
> Thank you
> 
> # mm = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
> 
> ```
> mm = [[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]]

You can just do something like this:

mm = []  
size.to\_i.times { mm \<\< Array.new(size.to\_i,0) }

Incidentally, have you looked into the numerous Matrix/math libs  
already defined in Ruby?

– Thomas A.

---

<div class="post-metadata">

**Author:** ![ribes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f4b2a3/32.png) [@ribes](https://www.ruby-forum.com/u/ribes)\
**Post date:** [October 28, 2007, 11:47pm UTC](https://www.ruby-forum.com/t/how-do-i-define-a-dynamic-matrix-or/118562/3 "2007-10-28T23:47:28Z")

</div>

Victor R. wrote:

> …I need to be  
> able to define my matrix dynamically and on demand.  
> Can anyone tell me how to do that?

Try this:

#get input:  
print 'rows: ’  
rows = Integer(gets)  
print 'columns: ’  
cols = Integer(gets)

#create matrix:  
matrix = []  
rows.times do |i|  
matrix \<\< Array.new(cols, 0)  
end

p matrix

Or this:

require ‘matrix’

print "size: "  
size = Integer(gets)

m = Matrix.zero(size)  
p m

---

<div class="post-metadata">

**Author:** ![ribes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f4b2a3/32.png) [@ribes](https://www.ruby-forum.com/u/ribes)\
**Post date:** [October 29, 2007, 12:06am UTC](https://www.ruby-forum.com/t/how-do-i-define-a-dynamic-matrix-or/118562/4 "2007-10-29T00:06:09Z")

</div>

On 10/28/07, Victor R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> For a 5 x 5 I define :  
> Thank you
> 
> # mm = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
> 
> integer part, giving us the center in row 0  
> end
> 
> ```
> puts " "
> end
> 
> ```
> 
> end # End method mag
> 
> # We start here
> 
> p1 = ARGV[0].to\_i
> 
> mag( p1 )

irb\> odd\_matrix\_number = 7  
irb\> my\_matrix - Array.new(odd\_matrix\_number) {  
Array.new(odd\_matrix\_number) {0} }

I’m not sure exactly what is your assignment, but it might be good to  
create a MagicSquare object.

My half a cent.

Todd

---

<div class="post-metadata">

**Author:** ![ribes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f4b2a3/32.png) [@ribes](https://www.ruby-forum.com/u/ribes)\
**Post date:** [October 28, 2007, 11:55pm UTC](https://www.ruby-forum.com/t/how-do-i-define-a-dynamic-matrix-or/118562/5 "2007-10-28T23:55:39Z")

</div>

Many thanks to Thomas and 7stud!  
I will try your suggestions!

Thanks again

Victor

---

<div class="post-metadata">

**Author:** ![ribes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f4b2a3/32.png) [@ribes](https://www.ruby-forum.com/u/ribes)\
**Post date:** [October 29, 2007, 12:10am UTC](https://www.ruby-forum.com/t/how-do-i-define-a-dynamic-matrix-or/118562/6 "2007-10-29T00:10:50Z")

</div>

On 10/28/07, Todd B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> irb\> odd\_matrix\_number = 7  
> irb\> my\_matrix - Array.new(odd\_matrix\_number) {  
> Array.new(odd\_matrix\_number) {0} }
> 
> I’m not sure exactly what is your assignment, but it might be good to  
> create a MagicSquare object.
> 
> My half a cent.
> 
> Todd

That is supposed to be…

irb\> my\_matrix = Array.new(odd\_matrix\_number) {  
Array.new(odd\_matrix\_number) {0} }

(“=” in place of the original “-”)

Somebody get me away from this particular keyboard 🙂

Todd

---

<div class="post-metadata">

**Author:** ![ribes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f4b2a3/32.png) [@ribes](https://www.ruby-forum.com/u/ribes)\
**Post date:** [October 29, 2007, 12:17am UTC](https://www.ruby-forum.com/t/how-do-i-define-a-dynamic-matrix-or/118562/7 "2007-10-29T00:17:50Z")

</div>

Thank you gents, for your suggestions and help.

Victor
