Populate (an array or a matrix) from a vector

Discussion in 'Ruby' started by Paul A., Apr 25, 2010.

  1. Paul A.

    Paul A. Guest

    Hello,

    I have a simple, but very tricky, problem. I would like to fix it using
    an array. But matix are possibly more appropriate (even if matrix can
    not have 3 dimensional values, I think).

    I would like to populate a table (of 2 or 3 dimensions), from a vector.

    I think the best way to explain myself is to put some examples, with an
    array such as:

    >> # thanks to Rolf Pedersen's function (http://www.ruby-forum.com/topic/208620)
    >> table = make_array(3, 5)


    And now, if the seeder vector is: [1, 0], the result should be:

    => [
    => [4, 3, 2, 1, 0],
    => [4, 3, 2, 1, 0],
    => [4, 3, 2, 1, 0]
    => ]

    If the vector is: [-1, 0]

    => [
    => [0, 1, 2, 3, 4],
    => [0, 1, 2, 3, 4],
    => [0, 1, 2, 3, 4]
    => ]

    If the vector is: [-2, 0]

    => [
    => [0, 0, 1, 1, 2],
    => [0, 0, 1, 1, 2],
    => [0, 0, 1, 1, 2]
    => ]

    If the vector is: [1, 1]

    => [
    => [2, 2, 2, 1, 0],
    => [1, 1, 1, 1, 0],
    => [0, 0, 0, 0, 0]
    => ]

    If the vector is: [0, 1]

    => [
    => [2, 2, 2, 2, 2],
    => [1, 1, 1, 1, 1],
    => [0, 0, 0, 0, 0]
    => ]

    Have you got a idea, or a plan? :s
    Any comments are welcome. Thank you.

    Best regards.
    --
    Posted via http://www.ruby-forum.com/.
     
    Paul A., Apr 25, 2010
    #1
    1. Advertising

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

    Tip: Did you check out the Matrix and Vector classes in the Ruby Standard
    Library?
    http://ruby-doc.org/core/classes/Matrix.html
    http://ruby-doc.org/core/classes/Vector.html

    Best regards,
    -Rolf

    On Sun, Apr 25, 2010 at 3:10 PM, Paul A. <> wrote:

    > Hello,
    >
    > I have a simple, but very tricky, problem. I would like to fix it using
    > an array. But matix are possibly more appropriate (even if matrix can
    > not have 3 dimensional values, I think).
    >
    > I would like to populate a table (of 2 or 3 dimensions), from a vector.
    >
    > I think the best way to explain myself is to put some examples, with an
    > array such as:
    >
    > >> # thanks to Rolf Pedersen's function (

    > http://www.ruby-forum.com/topic/208620)
    > >> table = make_array(3, 5)

    >
    > And now, if the seeder vector is: [1, 0], the result should be:
    >
    > => [
    > => [4, 3, 2, 1, 0],
    > => [4, 3, 2, 1, 0],
    > => [4, 3, 2, 1, 0]
    > => ]
    >
    > If the vector is: [-1, 0]
    >
    > => [
    > => [0, 1, 2, 3, 4],
    > => [0, 1, 2, 3, 4],
    > => [0, 1, 2, 3, 4]
    > => ]
    >
    > If the vector is: [-2, 0]
    >
    > => [
    > => [0, 0, 1, 1, 2],
    > => [0, 0, 1, 1, 2],
    > => [0, 0, 1, 1, 2]
    > => ]
    >
    > If the vector is: [1, 1]
    >
    > => [
    > => [2, 2, 2, 1, 0],
    > => [1, 1, 1, 1, 0],
    > => [0, 0, 0, 0, 0]
    > => ]
    >
    > If the vector is: [0, 1]
    >
    > => [
    > => [2, 2, 2, 2, 2],
    > => [1, 1, 1, 1, 1],
    > => [0, 0, 0, 0, 0]
    > => ]
    >
    > Have you got a idea, or a plan? :s
    > Any comments are welcome. Thank you.
    >
    > Best regards.
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >
     
    Rolf Pedersen, Apr 25, 2010
    #2
    1. Advertising

  3. Paul A.

    Paul A. Guest

    Paul A., Apr 25, 2010
    #3
  4. On Sun, Apr 25, 2010 at 2:10 PM, Paul A. <> wrote:
    > ... I have a simple, but very tricky, problem. =A0I would like to fix it =

    using
    > an array. =A0But matix are possibly more appropriate (even if matrix can
    > not have 3 dimensional values, I think). ..


    I must confess that from your examples, I'm not at all sure how you
    want to populate the "array" given the vector, so the following may
    not be any help at all. But: if you want multidimensional numerical
    arrays, then NArray might be useful.

    http://narray.rubyforge.org
    NArray is an Numerical N-dimensional Array class. Supported element
    types are 1/2/4-byte Integer, single/double-precision Real/Complex,
    and Ruby Object. This extension library incorporates fast calculation
    and easy manipulation of large numerical arrays into the Ruby
    language. NArray has features similar to NumPy, but NArray has vector
    and matrix subclasses. ...

    I haven't used it much myself, and I haven't used it recently. But I
    did manage to install and use it without using gems, so if I can do it
    that way it can't be all that difficult to install !
     
    Colin Bartlett, Apr 26, 2010
    #4
  5. Hi,

    Paul A. wrote:
    > And now, if the seeder vector is: [1, 0], the result should be:
    >
    > => [
    > => [4, 3, 2, 1, 0],
    > => [4, 3, 2, 1, 0],
    > => [4, 3, 2, 1, 0]
    > => ]


    I guessed your intention and wrote it using NArray:

    require 'narray'

    def populate( shape, v )
    ndim = shape.size
    max = shape.max
    a = NArray.int(ndim,*shape)
    shape.each_with_index do |sz,i|
    if v == 0
    a[i,false] = max
    else
    r = [1]*ndim
    r = sz
    x = NArray.int(sz).indgen!
    x = sz-1-x if v > 0
    x /= v.abs
    a[i,false] = x.reshape!(*r)
    end
    end
    a.min(0)
    end

    populate( [5,3], [1,0] )
    => NArray.int(5,3):
    [ [ 4, 3, 2, 1, 0 ],
    [ 4, 3, 2, 1, 0 ],
    [ 4, 3, 2, 1, 0 ] ]

    populate( [5,3], [-1,0] )
    => NArray.int(5,3):
    [ [ 0, 1, 2, 3, 4 ],
    [ 0, 1, 2, 3, 4 ],
    [ 0, 1, 2, 3, 4 ] ]

    populate( [5,3], [-2,0] )
    => NArray.int(5,3):
    [ [ 0, 0, 1, 1, 2 ],
    [ 0, 0, 1, 1, 2 ],
    [ 0, 0, 1, 1, 2 ] ]

    populate( [5,3], [1,1] )
    => NArray.int(5,3):
    [ [ 2, 2, 2, 1, 0 ],
    [ 1, 1, 1, 1, 0 ],
    [ 0, 0, 0, 0, 0 ] ]

    populate( [5,3], [0,1] )
    => NArray.int(5,3):
    [ [ 2, 2, 2, 2, 2 ],
    [ 1, 1, 1, 1, 1 ],
    [ 0, 0, 0, 0, 0 ] ]

    populate( [5,3,3], [2,1,1] )
    => NArray.int(5,3,3):
    [ [ [ 2, 1, 1, 0, 0 ],
    [ 1, 1, 1, 0, 0 ],
    [ 0, 0, 0, 0, 0 ] ],
    [ [ 1, 1, 1, 0, 0 ],
    [ 1, 1, 1, 0, 0 ],
    [ 0, 0, 0, 0, 0 ] ],
    [ [ 0, 0, 0, 0, 0 ],
    [ 0, 0, 0, 0, 0 ],
    [ 0, 0, 0, 0, 0 ] ] ]

    Masahiro Tanaka
     
    Masahiro TANAKA, Apr 27, 2010
    #5
  6. Paul A.

    Paul A. Guest

    Thank you very much, Masahiro Tanaka! Your response is exactly what I
    dreamed of doing. And your numerical n-dimensional array class is
    absolutly awesome! I'm glad to continue coding with!

    Thanks again.
    Best regards.
    --
    Posted via http://www.ruby-forum.com/.
     
    Paul A., Apr 27, 2010
    #6
    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. pmatos
    Replies:
    6
    Views:
    23,938
  2. Replies:
    8
    Views:
    1,961
    Csaba
    Feb 18, 2006
  3. Javier
    Replies:
    2
    Views:
    584
    James Kanze
    Sep 4, 2007
  4. Holgerson

    Matrix*Vector and Vector*Matrix

    Holgerson, Oct 25, 2007, in forum: C++
    Replies:
    3
    Views:
    420
    Holgerson
    Oct 26, 2007
  5. Enjoy Life
    Replies:
    2
    Views:
    291
    Roland Hall
    Feb 23, 2005
Loading...

Share This Page