# Matrix in Ruby

**URL:** <https://www.ruby-forum.com/t/matrix-in-ruby/79826>\
**Category:** Ruby\
**Created:** [March 11, 2006, 9:56am UTC](https://www.ruby-forum.com/t/matrix-in-ruby/79826 "2006-03-11T09:56:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pool007](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3bc359/32.png) [@pool007](https://www.ruby-forum.com/u/pool007)\
**Post date:** [March 11, 2006, 9:56am UTC](https://www.ruby-forum.com/t/matrix-in-ruby/79826/1 "2006-03-11T09:56:01Z")

</div>

What is the best way to create a matrix whose size is m x n?  
The value of m and n may be different, and the values in each cell  
should be zeroed.

Can anybody help me?

Sincerely,  
Minkoo S.

---

<div class="post-metadata">

**Author:** ![pool007](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3bc359/32.png) [@pool007](https://www.ruby-forum.com/u/pool007)\
**Post date:** [March 11, 2006, 1:31pm UTC](https://www.ruby-forum.com/t/matrix-in-ruby/79826/2 "2006-03-11T13:31:05Z")

</div>

I’ve found it by myself:

a = Matrix[(0…3).map { Array.new(5) }]

---

<div class="post-metadata">

**Author:** ![pool007](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3bc359/32.png) [@pool007](https://www.ruby-forum.com/u/pool007)\
**Post date:** [March 11, 2006, 4:16pm UTC](https://www.ruby-forum.com/t/matrix-in-ruby/79826/3 "2006-03-11T16:16:49Z")

</div>

On Sat, 11 Mar 2006, Minkoo S. wrote:

> I’ve found it by myself:
> 
> a = Matrix[(0…3).map { Array.new(5) }]

if you’re going to be working with large matrices and doing lots of  
number  
crunching you’ll want

require ‘narray’

a = NArray.int 3, 5

it’s on the raa.

cheers.

-a
