# Array.transpose on \] or \[\[\],\[\]\]

**URL:** <https://www.ruby-forum.com/t/array-transpose-on-or/174885>\
**Category:** Ruby\
**Created:** [September 3, 2009, 2:46pm UTC](https://www.ruby-forum.com/t/array-transpose-on-or/174885 "2009-09-03T14:46:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ralf\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/13edae/32.png) [@Ralf\_M](https://www.ruby-forum.com/u/Ralf_M)\
**Post date:** [September 3, 2009, 2:46pm UTC](https://www.ruby-forum.com/t/array-transpose-on-or/174885/1 "2009-09-03T14:46:28Z")

</div>

Hi,

I wondered why [[]].transpose = [] and [[],[]].transpose = []  
I expected them to be [[]], because the result would still be an array  
of arrays.

Or did I miss anything about these special cases?

Best regards  
Ralf

---

<div class="post-metadata">

**Author:** ![Ralf\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/13edae/32.png) [@Ralf\_M](https://www.ruby-forum.com/u/Ralf_M)\
**Post date:** [September 3, 2009, 5:55pm UTC](https://www.ruby-forum.com/t/array-transpose-on-or/174885/2 "2009-09-03T17:55:36Z")

</div>

Ralf M. wrote:

> I wondered why [[]].transpose = [] and [[],[]].transpose = []  
> I expected them to be [[]], because the result would still be an array  
> of arrays.

You can think of [] as an array of 0 arrays in this case.  
The number of arrays in the array returned by transpose equals the  
number  
of items in each subarray of the original array. So if that number is 0,  
it  
makes sense that the returned array would also have 0 subarrays.

HTH,  
Sebastian

---

<div class="post-metadata">

**Author:** ![Ralf\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/13edae/32.png) [@Ralf\_M](https://www.ruby-forum.com/u/Ralf_M)\
**Post date:** [September 4, 2009, 11:25am UTC](https://www.ruby-forum.com/t/array-transpose-on-or/174885/3 "2009-09-04T11:25:56Z")

</div>

Sebastian H. wrote:

> makes sense that the returned array would also have 0 subarrays.
> 
> HTH,  
> Sebastian  
> Hi Sebastian,  
> sure, but [[]] has a subarray. so, shouldn’t [[]].transpose have one  
> too?  
> To point it out in a more general way: I thought, if ‘transpose’ can be  
> applied to an array, it should behave like an involution:  
> a.transpose.transpose = a.

anyway, one more thing to remember…

Thanks and best regards  
Ralf

---

<div class="post-metadata">

**Author:** ![Ralf\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/13edae/32.png) [@Ralf\_M](https://www.ruby-forum.com/u/Ralf_M)\
**Post date:** [September 4, 2009, 1:00pm UTC](https://www.ruby-forum.com/t/array-transpose-on-or/174885/4 "2009-09-04T13:00:46Z")

</div>

2009/9/4 Ralf M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > You can think of [] as an array of 0 arrays in this case.  
> > The number of arrays in the array returned by transpose equals the number  
> > of items in each subarray of the original array. So if that number is 0,  
> > it  
> > makes sense that the returned array would also have 0 subarrays.

> sure, but [[]] has a subarray. so, shouldn’t [[]].transpose have one too? To  
> point it out in a more general way: I thought, if ‘transpose’ can be applied  
> to an array, it should behave like an involution: a.transpose.transpose = a.
> 
> anyway, one more thing to remember…

No, [[]].transpose should not have a sub array: [[]] must be seen as a  
matrix with one row with no columns. If you transpose it you get a  
matrix with no row with one column hence []. It does not matter when  
you exchange terms “row” and “columns” in the sentence above.

Personally I would be using a specialized Matrix class anyway if you  
need to work with matrices.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![Ralf\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/13edae/32.png) [@Ralf\_M](https://www.ruby-forum.com/u/Ralf_M)\
**Post date:** [September 4, 2009, 12:36pm UTC](https://www.ruby-forum.com/t/array-transpose-on-or/174885/5 "2009-09-04T12:36:46Z")

</div>

Am Freitag 04 September 2009 11:25:04 schrieb Ralf M.:

> sure, but [[]] has a subarray. so, shouldn’t [[]].transpose have one too?

Well, [[1],[2]] has two subarrays so should [[1],[2]].transpose have two  
subarrays too?  
As I said, the number of subarrays in the result equals the number of  
items  
per subarray in the source. I find this completely consistent.

> To point it out in a more general way: I thought, if ‘transpose’ can be  
> applied to an array, it should behave like an involution:  
> a.transpose.transpose = a.

If both [[]].transpose and [[],[]].transpose would return [[]], that  
still  
wouldn’t be true (because then [[],[]].transpose.transpose would be  
[[]]).  
The only way to ensure that [[],[]].transpose.transpose == [[],[]],  
would be  
to make transpose on an array of empty arrays to return self, which  
would be  
totally inconsistent with how transpose usually works.

---

<div class="post-metadata">

**Author:** ![Ralf\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/13edae/32.png) [@Ralf\_M](https://www.ruby-forum.com/u/Ralf_M)\
**Post date:** [September 4, 2009, 1:07pm UTC](https://www.ruby-forum.com/t/array-transpose-on-or/174885/6 "2009-09-04T13:07:48Z")

</div>

Sebastian H. wrote:

> > To point it out in a more general way: I thought, if ‘transpose’ can be  
> > applied to an array, it should behave like an involution:  
> > a.transpose.transpose = a.
> 
> If both [[]].transpose and [[],[]].transpose would return [[]], that still  
> wouldn’t be true (because then [[],[]].transpose.transpose would be [[]]).

Yes, I was contradictory to myself ;(  
And a.transpose.transpose = a is true for ‘non-degenerated’ a.  
Have to program a bit more careful (not too much guessing).

thanks again  
ralf
