From: Masahiro TANAKA Date: 2010-04-27T22:52:19+09:00 Subject: Re: Populate (an array or a matrix) from a vector 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