Forum: Ruby Initializing an NxN array

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
64a808c19ac542b14c4021dcb9484a37?d=identicon&s=25 Ruby Student (Guest)
on 2009-03-19 17:09
(Received via mailing list)
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
Fbb4d027695dfdf76bf448b15d7e306a?d=identicon&s=25 matt neuburg (Guest)
on 2009-03-19 17:55
(Received via mailing list)
Ruby Student <ruby.student@gmail.com> 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.
Eacf5ed623e72db0b87707baabd6f21a?d=identicon&s=25 Adam Gardner (adamgardner)
on 2009-03-19 18:31
Ruby Student 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}}
E22bfc507fcbbd807a7b6519aa9070f7?d=identicon&s=25 Marcelo (Guest)
on 2009-03-19 18:33
(Received via mailing list)
On Thu, Mar 19, 2009 at 10:05, Ruby Student <ruby.student@gmail.com>
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.
Bb4976df206152ce93aad43f1eb8faa0?d=identicon&s=25 Evan Farrar (evanfarrar)
on 2009-03-19 18:37
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]]
Bb4976df206152ce93aad43f1eb8faa0?d=identicon&s=25 Evan Farrar (evanfarrar)
on 2009-03-19 18:38
Evan Farrar 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!
64a808c19ac542b14c4021dcb9484a37?d=identicon&s=25 Ruby Student (Guest)
on 2009-03-19 19:31
(Received via mailing list)
On Thu, Mar 19, 2009 at 1:36 PM, Evan Farrar <evanfarrar@gmail.com>
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/.
>
>

Thanks to all, your help is appreicated!
This topic is locked and can not be replied to.