# Initializing an NxN array

**URL:** <https://www.ruby-forum.com/t/initializing-an-nxn-array/163370>\
**Category:** Ruby\
**Created:** [March 19, 2009, 5:09pm UTC](https://www.ruby-forum.com/t/initializing-an-nxn-array/163370 "2009-03-19T17:09:24Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![rubina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e47c2d/32.png) [@rubina](https://www.ruby-forum.com/u/rubina)\
**Post date:** [March 19, 2009, 5:09pm UTC](https://www.ruby-forum.com/t/initializing-an-nxn-array/163370/1 "2009-03-19T17:09:24Z")

</div>

Team,

What is the easiest way to initialize a square matrix?  
For example, to initialize a 3x3 array elements to 0, I am doing what  
you  
see below. But I am not sure how to proceed if, for instance, I want a  
NxN  
array where N \> 10 or a huge value?  
I played a bit on IRB but could not find the way to do it easily.

irb(main):008:0\> ary = Array.new(9,[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]]

Thank you

---

<div class="post-metadata">

**Author:** ![rubina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e47c2d/32.png) [@rubina](https://www.ruby-forum.com/u/rubina)\
**Post date:** [March 19, 2009, 5:55pm UTC](https://www.ruby-forum.com/t/initializing-an-nxn-array/163370/2 "2009-03-19T17:55:44Z")

</div>

Ruby S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ary = Array.new(9,[0,0,0])

Tread carefully, Grasshopper:

ary = Array.new(9,[0,0,0])  
ary[0][0] = “surprise!”  
p ary

🙂

m.

---

<div class="post-metadata">

**Author:** ![rubina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e47c2d/32.png) [@rubina](https://www.ruby-forum.com/u/rubina)\
**Post date:** [March 19, 2009, 6:31pm UTC](https://www.ruby-forum.com/t/initializing-an-nxn-array/163370/3 "2009-03-19T18:31:35Z")

</div>

Ruby S. wrote:

> Team,
> 
> What is the easiest way to initialize a square matrix?  
> For example, to initialize a 3x3 array elements to 0, I am doing what  
> you  
> see below. But I am not sure how to proceed if, for instance, I want a  
> NxN  
> array where N \> 10 or a huge value?  
> I played a bit on IRB but could not find the way to do it easily.
> 
> irb(main):008:0\> ary = Array.new(9,[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]]
> 
> Thank you

Wouldn’t the best solution be:

require ‘Matrix’  
a = Matrix.zero(3)

?

Assuming you actually want to use it for Matrix math and not as just a  
2D array.

If you want a 2D array, I’d do ary = Array.new(3) {|row| Array.new(3)  
{|col| 0}}

---

<div class="post-metadata">

**Author:** ![rubina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e47c2d/32.png) [@rubina](https://www.ruby-forum.com/u/rubina)\
**Post date:** [March 19, 2009, 6:37pm UTC](https://www.ruby-forum.com/t/initializing-an-nxn-array/163370/4 "2009-03-19T18:37:10Z")

</div>

Since \* is defined on array, you can start with a 1x1 array and multiply  
to the size you need.

n = 3;  
ary = [[0]\*n]\*n

results in:  
ary =\> [[0, 0, 0], [0, 0, 0], [0, 0, 0]]

---

<div class="post-metadata">

**Author:** ![rubina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e47c2d/32.png) [@rubina](https://www.ruby-forum.com/u/rubina)\
**Post date:** [March 19, 2009, 6:38pm UTC](https://www.ruby-forum.com/t/initializing-an-nxn-array/163370/5 "2009-03-19T18:38:56Z")

</div>

Evan F. wrote:

> Since \* is defined on array, you can start with a 1x1 array and multiply  
> to the size you need.
> 
> n = 3;  
> ary = [[0]\*n]\*n
> 
> results in:  
> ary =\> [[0, 0, 0], [0, 0, 0], [0, 0, 0]]

whoops! that has the same problem mentioned above:  
irb(main):026:0\> ary[0][0] = :foo; ary  
=\> [[:foo, 0, 0], [:foo, 0, 0], [:foo, 0, 0]]

I retract!

---

<div class="post-metadata">

**Author:** ![rubina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e47c2d/32.png) [@rubina](https://www.ruby-forum.com/u/rubina)\
**Post date:** [March 19, 2009, 6:33pm UTC](https://www.ruby-forum.com/t/initializing-an-nxn-array/163370/6 "2009-03-19T18:33:55Z")

</div>

On Thu, Mar 19, 2009 at 10:05, Ruby S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> For example, to initialize a 3x3 array elements to 0, I am doing what you  
> see below. But I am not sure how to proceed if, for instance, I want a NxN  
> array where N \> 10 or a huge value?

N=3  
Array.new(N) { Array.new(N,0) }

-m.

---

<div class="post-metadata">

**Author:** ![rubina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e47c2d/32.png) [@rubina](https://www.ruby-forum.com/u/rubina)\
**Post date:** [March 19, 2009, 7:31pm UTC](https://www.ruby-forum.com/t/initializing-an-nxn-array/163370/7 "2009-03-19T19:31:28Z")

</div>

On Thu, Mar 19, 2009 at 1:36 PM, Evan F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> whoops! that has the same problem mentioned above:  
> irb(main):026:0\> ary[0][0] = :foo; ary  
> =\> [[:foo, 0, 0], [:foo, 0, 0], [:foo, 0, 0]]
> 
> ## I retract!
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Thanks to all, your help is appreicated!
