# Populate (an array or a matrix) from a vector

**URL:** <https://www.ruby-forum.com/t/populate-an-array-or-a-matrix-from-a-vector/187548>\
**Category:** Ruby\
**Created:** [April 25, 2010, 3:10pm UTC](https://www.ruby-forum.com/t/populate-an-array-or-a-matrix-from-a-vector/187548 "2010-04-25T15:10:03Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![judge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@judge](https://www.ruby-forum.com/u/judge)\
**Post date:** [April 25, 2010, 3:10pm UTC](https://www.ruby-forum.com/t/populate-an-array-or-a-matrix-from-a-vector/187548/1 "2010-04-25T15:10:03Z")

</div>

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 P.'s function ([Metaprogramming - Array initialization - Ruby - Ruby-Forum](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.

---

<div class="post-metadata">

**Author:** ![judge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@judge](https://www.ruby-forum.com/u/judge)\
**Post date:** [April 25, 2010, 4:09pm UTC](https://www.ruby-forum.com/t/populate-an-array-or-a-matrix-from-a-vector/187548/2 "2010-04-25T16:09:30Z")

</div>

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/Matrix.html)  
[http://ruby-doc.org/core/classes/Vector.html](http://ruby-doc.org/core/classes/Vector.html)

Best regards,  
-Rolf

---

<div class="post-metadata">

**Author:** ![judge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@judge](https://www.ruby-forum.com/u/judge)\
**Post date:** [April 25, 2010, 5:54pm UTC](https://www.ruby-forum.com/t/populate-an-array-or-a-matrix-from-a-vector/187548/3 "2010-04-25T17:54:27Z")

</div>

Rolf P. wrote:

> 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/Matrix.html)  
> [http://ruby-doc.org/core/classes/Vector.html](http://ruby-doc.org/core/classes/Vector.html)
> 
> Best regards,  
> -Rolf

Hi Rolf,

Yes, but I really don’t see how to process with. And matrix are limited  
to 2 dimensional arrays. But even just for 2D, it’s looks like tricky.  
O\_o

---

<div class="post-metadata">

**Author:** ![judge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@judge](https://www.ruby-forum.com/u/judge)\
**Post date:** [April 27, 2010, 3:53pm UTC](https://www.ruby-forum.com/t/populate-an-array-or-a-matrix-from-a-vector/187548/4 "2010-04-27T15:53:09Z")

</div>

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[i] == 0  
a[i,false] = max  
else  
r = [1]\*ndim  
r[i] = sz  
x = NArray.int(sz).indgen!  
x = sz-1-x if v[i] \> 0  
x /= v[i].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

---

<div class="post-metadata">

**Author:** ![judge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@judge](https://www.ruby-forum.com/u/judge)\
**Post date:** [April 27, 2010, 9:34pm UTC](https://www.ruby-forum.com/t/populate-an-array-or-a-matrix-from-a-vector/187548/5 "2010-04-27T21:34:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![judge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@judge](https://www.ruby-forum.com/u/judge)\
**Post date:** [April 26, 2010, 6:22pm UTC](https://www.ruby-forum.com/t/populate-an-array-or-a-matrix-from-a-vector/187548/6 "2010-04-26T18:22:18Z")

</div>

On Sun, Apr 25, 2010 at 2:10 PM, Paul A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> … 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 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](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 !
