Initializing an NxN array

Discussion in 'Ruby' started by Ruby Student, Mar 19, 2009.

  1. Ruby Student

    Ruby Student Guest

    [Note: parts of this message were removed to make it a legal post.]

    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

    --
    Ruby Student
    Ruby Student, Mar 19, 2009
    #1
    1. Advertising

  2. Ruby Student

    matt neuburg Guest

    matt neuburg, Mar 19, 2009
    #2
    1. Advertising

  3. Ruby Student

    Adam Gardner Guest

    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}}
    --
    Posted via http://www.ruby-forum.com/.
    Adam Gardner, Mar 19, 2009
    #3
  4. Ruby Student

    Marcelo Guest

    On Thu, Mar 19, 2009 at 10:05, Ruby Student <> 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.
    Marcelo, Mar 19, 2009
    #4
  5. Ruby Student

    Evan Farrar Guest

    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]]
    --
    Posted via http://www.ruby-forum.com/.
    Evan Farrar, Mar 19, 2009
    #5
  6. Ruby Student

    Evan Farrar Guest

    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!
    --
    Posted via http://www.ruby-forum.com/.
    Evan Farrar, Mar 19, 2009
    #6
  7. Ruby Student

    Ruby Student Guest

    [Note: parts of this message were removed to make it a legal post.]

    On Thu, Mar 19, 2009 at 1:36 PM, Evan Farrar <> wrote:

    > 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!
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >


    Thanks to all, your help is appreicated!

    --
    Ruby Student
    Ruby Student, Mar 19, 2009
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. newby1

    initializing an array

    newby1, Jul 3, 2004, in forum: Java
    Replies:
    3
    Views:
    444
    Hal Rosser
    Jul 4, 2004
  2. Replies:
    11
    Views:
    1,100
  3. MIA
    Replies:
    7
    Views:
    501
  4. J. Campbell
    Replies:
    4
    Views:
    8,405
    Gianni Mariani
    Jan 29, 2004
  5. Steven T. Hatton
    Replies:
    1
    Views:
    6,619
    Siemel Naran
    Apr 19, 2004
Loading...

Share This Page