8x8 matrix

Is there an easier way to create a 8x8 matrix then:
a=Array.new
0.upto(7) {|i| a[i]=Array.new(8,0)} ?

If I use
a=Array.new(8,Array.new(8,0))

an assignment like
a[0][0]=1

will result in:
[[1, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0,
0], [1, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0,
0, 0], [1, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0, 0]]

so long…

···


“Die Geschichte wiederholt sich nicht, wohl aber die Leichtfertigkeit,
mit der sie gemacht wird.” [W.Weidner]

Sebastian Ruhs wrote:

Is there an easier way to create a 8x8 matrix then:
a=Array.new
0.upto(7) {|i| a[i]=Array.new(8,0)} ?

If I use
a=Array.new(8,Array.new(8,0))

an assignment like
a[0][0]=1

will result in:
[[1, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0,
0], [1, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0,
0, 0], [1, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0, 0]]

so long…


“Die Geschichte wiederholt sich nicht, wohl aber die Leichtfertigkeit,
mit der sie gemacht wird.” [W.Weidner]

require “matrix”

m = Matrix.I(8) # or Matrix.zero(8) if that’s what you want

Regards,

Dan

there was a solution posted a couple days ago using #map.

···

-----Original Message-----
From: Sebastian Ruhs [mailto:RembrandtAkaDodger@gmx.de]
Sent: Tuesday, December 10, 2002 12:14 PM
To: ruby-talk ML
Subject: 8x8 matrix

Is there an easier way to create a 8x8 matrix then:
a=Array.new
0.upto(7) {|i| a[i]=Array.new(8,0)} ?

about using map (from The Ruby Way),
you can do:

matrix = (0…7).map { (0…7).map {[0]} }

···

On Wed, 11 Dec 2002 02:39:01 +0900, Mike Campbell wrote:

there was a solution posted a couple days ago using #map.

-----Original Message-----
From: Sebastian Ruhs [mailto:RembrandtAkaDodger@gmx.de]
Sent: Tuesday, December 10, 2002 12:14 PM
To: ruby-talk ML
Subject: 8x8 matrix

Is there an easier way to create a 8x8 matrix then:
a=Array.new
0.upto(7) {|i| a[i]=Array.new(8,0)} ?

sorry, I want write:
matrix = (0…7).map { (0…7).map {0} }
nb: the init element isn’t an array!

···

On Tue, 10 Dec 2002 18:43:00 +0100, Ludo wrote:

about using map (from The Ruby Way),
you can do:

matrix = (0…7).map { (0…7).map {[0]} }

On Wed, 11 Dec 2002 02:39:01 +0900, Mike Campbell wrote:

there was a solution posted a couple days ago using #map.

-----Original Message-----
From: Sebastian Ruhs [mailto:RembrandtAkaDodger@gmx.de]
Sent: Tuesday, December 10, 2002 12:14 PM
To: ruby-talk ML
Subject: 8x8 matrix

Is there an easier way to create a 8x8 matrix then:
a=Array.new
0.upto(7) {|i| a[i]=Array.new(8,0)} ?