# Single to multi dimension array conversion

**URL:** <https://www.ruby-forum.com/t/single-to-multi-dimension-array-conversion/165379>\
**Category:** Ruby\
**Created:** [April 13, 2009, 12:57pm UTC](https://www.ruby-forum.com/t/single-to-multi-dimension-array-conversion/165379 "2009-04-13T12:57:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ashikalim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/9dc877/32.png) [@ashikalim](https://www.ruby-forum.com/u/ashikalim)\
**Post date:** [April 13, 2009, 12:57pm UTC](https://www.ruby-forum.com/t/single-to-multi-dimension-array-conversion/165379/1 "2009-04-13T12:57:41Z")

</div>

Can any one tell me converting single to multi dimension array  
conversion

For example ,  
a = [1,2,3,4,5,6,7,8,9,10]  
a.( 2,3 )

```
  Output,

```

here ,  
2 means dimension No  
3 means elements in each dimension

---

<div class="post-metadata">

**Author:** ![ashikalim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/9dc877/32.png) [@ashikalim](https://www.ruby-forum.com/u/ashikalim)\
**Post date:** [April 13, 2009, 12:59pm UTC](https://www.ruby-forum.com/t/single-to-multi-dimension-array-conversion/165379/2 "2009-04-13T12:59:45Z")

</div>

Ashikali A. wrote:

> Can any one tell me converting single to multi dimension array  
> conversion
> 
> For example ,  
> a = [1,2,3,4,5,6,7,8,9,10]  
> a.( 2,3 )
> 
> ```
> Output,
> 
> ```
> 
> here ,  
> 2 means dimension No  
> 3 means elements in each dimension

In above example output should be ,  
a = [[1,2,3],[4,5,6],[7,8,9] , [10] ]

---

<div class="post-metadata">

**Author:** ![ashikalim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/9dc877/32.png) [@ashikalim](https://www.ruby-forum.com/u/ashikalim)\
**Post date:** [April 13, 2009, 1:26pm UTC](https://www.ruby-forum.com/t/single-to-multi-dimension-array-conversion/165379/3 "2009-04-13T13:26:36Z")

</div>

2009/4/13 Ashikali A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> > here ,  
> > 2 means dimension No  
> > 3 means elements in each dimension
> 
> In above example output should be ,  
> a = [[1,2,3],[4,5,6],[7,8,9] , [10] ]

That has more than three elements along the outermost direction. It  
represents the matrix:

1 2 3  
4 5 6  
7 8 9  
10

So you can’t fit the data into a 3x3 space. You might need to specify  
the  
problem a bit more carefully.

---

<div class="post-metadata">

**Author:** ![ashikalim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/9dc877/32.png) [@ashikalim](https://www.ruby-forum.com/u/ashikalim)\
**Post date:** [April 13, 2009, 1:46pm UTC](https://www.ruby-forum.com/t/single-to-multi-dimension-array-conversion/165379/4 "2009-04-13T13:46:32Z")

</div>

2009/4/13 James C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> > > ```
> > > Output,
> > > 
> > > ```
> 
> That has more than three elements along the outermost direction. It  
> represents the matrix:
> 
> 1 2 3  
> 4 5 6  
> 7 8 9  
> 10

Although, here’s one possible and slightly more general solution:

class Array  
def multidim(\*sizes)  
split = sizes.inject { |a,b| a \* b }  
return self unless split  
result = []  
each\_slice(split) { |slice| result \<\< slice.multidim(\*sizes[1…-1])  
}  
result  
end  
end

With this, you specify how many you want in each dimension, so:

[1,2,3,4,5,6,7,8,9,10].multidim(3)  
#=\> [[1,2,3],[4,5,6],[7,8,9],[10]]

It works from the outside in, so for higher dimensions it works like  
this:

[1,2,3,4,5,6,7,8,9,10].multidim(3,2)  
#=\> [[ [1, 2],  
[3, 4],  
[5, 6]  
],  
[[7, 8],  
[9, 10]  
] ]
