From: CBlair1986 Date: 2006-11-18T18:15:04+09:00 Subject: Re: Splitting an array into quads and putting them back together again. On Nov 16, 7:37 am, "Martin DeMello" wrote: > On 11/16/06, Chris Blair wrote: > > > > > Thanks! I don't quite understand the code, right now, but it does work > > exactly how I wanted it. Sorry for the delay in responding, I haven't had > > access to the internet for a little while.Here's a quick explanation of how the code works: > > Ruby's multidimensional arrays are actually arrays of arrays. So if you have > > a1 = [[1,2],[5,6]] > b1 = [[3,4],[7,8]] > a2 = [[9,10],[13,14]] > b2 = [[11,12],[15,16]] > > and you want > > a = [[1,2,5,6], [3,4,7,8], [9,10,13,14], [11,12,15,16]] > > you need (i) a way to combine two submatrices side by side and (ii) a > way to combine them one on top of the other. > > Let's start with side by side, defining the | operator so that > > [[1, 2], > [5, 6]] | > [[3, 4], > [7, 8]] = > > [[1, 2, 3, 4], > [5, 6, 7, 8]] > > To do that we need to walk over each row of both arrays, in parallel, > and combine the rows into one big row. The #zip method steps over two > enumerables in parallel, returning pairs of elements, so that > > [a, b].zip([c,d]) #=> [[a, c], [b, d]] > > Let's take that for a spin: > > irb(main):099:0> [a1, b1, a2, b2].shape(2) > => [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]] > > and there we are. > > martin Thank you for the in-depth explanation. It really helped me grasp what you did. Honestly, I would never have thought about the problem as you did, so I see I've got a lot more to learn. Thank you again!