# How can i get that kind of transpose?

**URL:** <https://www.ruby-forum.com/t/how-can-i-get-that-kind-of-transpose/187917>\
**Category:** Ruby\
**Created:** [May 2, 2010, 3:59am UTC](https://www.ruby-forum.com/t/how-can-i-get-that-kind-of-transpose/187917 "2010-05-02T03:59:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![luofeiyu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9de0a6/32.png) [@luofeiyu](https://www.ruby-forum.com/u/luofeiyu)\
**Post date:** [May 2, 2010, 3:59am UTC](https://www.ruby-forum.com/t/how-can-i-get-that-kind-of-transpose/187917/1 "2010-05-02T03:59:13Z")

</div>

there is text1  
1,2,3,4  
1,2,3  
1,2  
1,2,3,4,5  
what i want to get is text2:  
1ï¼Œ1ï¼Œ1ï¼Œ1  
2ï¼Œ2ï¼Œ2ï¼Œ2  
3ï¼Œ3ï¼Œ 3  
4ï¼Œ 4  
5  
how to get it ?

---

<div class="post-metadata">

**Author:** ![luofeiyu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9de0a6/32.png) [@luofeiyu](https://www.ruby-forum.com/u/luofeiyu)\
**Post date:** [May 2, 2010, 4:18am UTC](https://www.ruby-forum.com/t/how-can-i-get-that-kind-of-transpose/187917/2 "2010-05-02T04:18:27Z")

</div>

On Sat, May 1, 2010 at 8:59 PM, Pen T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 5  
> how to get it ?

Try googling for ruby matrix rotate.

[http://snippets.dzone.com/tag/rotate](http://snippets.dzone.com/tag/rotate) came up in a 2 second search.

This is also a ruby quiz.  
[http://rubyquiz.strd6.com/quizzes/209-matrix-rotator](http://rubyquiz.strd6.com/quizzes/209-matrix-rotator)

Andrew McElroy

---

<div class="post-metadata">

**Author:** ![luofeiyu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9de0a6/32.png) [@luofeiyu](https://www.ruby-forum.com/u/luofeiyu)\
**Post date:** [May 2, 2010, 5:02am UTC](https://www.ruby-forum.com/t/how-can-i-get-that-kind-of-transpose/187917/3 "2010-05-02T05:02:56Z")

</div>

On Sun, May 2, 2010 at 9:59 AM, Pen T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> there is text1  
> 1,2,3,4  
> 1,2,3  
> 1,2  
> 1,2,3,4,5$  
> what i want to get is text2:  
> 1ï¼Œ1ï¼Œ1ï¼Œ1  
> 2ï¼Œ2ï¼Œ2ï¼Œ2  
> 3ï¼Œ3ï¼Œ Â 3\>\> Matrix[[1,2,3,4], [1,2,3], [1,2]]. transpose  
> =\> Matrix[[1, 1, 1], [2, 2, 2], [3, 3, nil], [4, nil, nil]]

> 4ï¼Œ Â 4  
> 5  
> how to get it ?

search for it first,

$ qri transpose

------------------------------------------------------ Multiple choices:

```
 Array#transpose, Matrix#transpose

```

so eg,

=\> Matrix[[1, 3, 5], [2, 4, 6]]

> > Matrix[[1,2,3,4], [1,2,3], [1,2]]. transpose  
> > =\> Matrix[[1, 1, 1], [2, 2, 2], [3, 3, nil], [4, nil, nil]]

kind regards -botp

---

<div class="post-metadata">

**Author:** ![luofeiyu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9de0a6/32.png) [@luofeiyu](https://www.ruby-forum.com/u/luofeiyu)\
**Post date:** [May 2, 2010, 10:36am UTC](https://www.ruby-forum.com/t/how-can-i-get-that-kind-of-transpose/187917/4 "2010-05-02T10:36:38Z")

</div>

Pen T. wrote:

> there is text1  
> 1,2,3,4  
> 1,2,3  
> 1,2  
> 1,2,3,4,5  
> what i want to get is text2:  
> 1ï¼Œ1ï¼Œ1ï¼Œ1  
> 2ï¼Œ2ï¼Œ2ï¼Œ2  
> 3ï¼Œ3ï¼Œ 3  
> 4ï¼Œ 4  
> 5

Is this what you want?

> > a = [[1,2,3,4,nil],[1,2,3,nil,nil],[1,2,nil,nil,nil],[1,2,3,4,5]]  
> > =\> [[1, 2, 3, 4, nil], [1, 2, 3, nil, nil], [1, 2, nil, nil, nil], [1,  
> > 2, 3, 4, 5]]
> 
> > a.transpose  
> > =\> [[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, nil, 3], [4, nil, nil, 4], [nil,  
> > nil, nil, 5]]

---

<div class="post-metadata">

**Author:** ![luofeiyu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9de0a6/32.png) [@luofeiyu](https://www.ruby-forum.com/u/luofeiyu)\
**Post date:** [May 3, 2010, 6:15pm UTC](https://www.ruby-forum.com/t/how-can-i-get-that-kind-of-transpose/187917/5 "2010-05-03T18:15:42Z")

</div>

On Sat, May 1, 2010 at 8:59 PM, Pen T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ## 5 how to get it ?
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

You need to be more clear:

First your names are “text1” and “text2” but your data is not quoted  
like  
text would be. It is split with commas like an array would be, but it is  
not  
bracketed like an array would be, and you named it text.

Second, your desired data is unclear, the spacing is off so that it is  
unclear which row the 4 belongs on, and the 5 seems to be on the wrong  
side.  
If this is what you actually desire, you need to explain the rules that  
determine this output, if not, then you need to double check your input.
