views:

69

answers:

2

Given say 2x3 and mx3 arrays (I have used NArray): how to construct a (2+m)x3 array, the concatenation of each. + or << do not keep the elements properly aligned. e.g.

a = [[1,2,3],[4,5,6]]
b = [[1,2,3,4],[5,6,7,8]]
# should be concatenated as:
# [[1,2,3,1,2,3,4],[4,5,6,5,6,7,8]]

Thanks.

A: 

Cary,

I have a loop which grows an array, each addition length is undetermined. I begin with an array and concatenate. One dimension is fixed, say 2. I am therefore looking for a way to grow the initial, say 2x3 array through concatenation to a 2x7, ... A becomes [A C], which becomes [A C D] ...

Thanks.

dnj
Welcome to SO. You should edit your question instead of giving more info in an "answer". Saying what you have tried can help. Reading on how to format could be helpful too.
Marc-André Lafortune
A: 

You can do it this way:

a = [[1,2,3],[4,5,6]]
b = [[1,2,3,4],[5,6,7,8]]
a.zip(b).map{|x, y| x+y}
# => [[1,2,3,1,2,3,4],[4,5,6,5,6,7,8]]
Marc-André Lafortune