Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Is there a quick method to transpose uneven lists without conditionals?

With:

Drop[Table[q, {10}], #] & /@ Range[10]

Thus the first list would have the first element of all the lists, the 2nd list would have all the 2nd elements of all the lists, etc. If there are no elements, skips. I have a feeling that this should incorporate Mathematica's Reap / Sow function, but unfamiliar.

share|improve this question
1  
The version-8 tag should be used to indicate questions, issues or functions that are specific for version 8, and not to indicate which system you use yourself. Many tags have a 'tag wiki', a small explanatory text that describe their usage. Hover your mouse above the tag to see it. –  Sjoerd C. de Vries Mar 28 '12 at 20:55
add comment

2 Answers 2

up vote 28 down vote accepted

Yes, but it is not trivial to comprehend. You would have to use the second argument of Flatten to implement a generalized transpose of uneven lists. For example:

(* Uneven list *)
list = Range ~Array~ 5
Out[1]= {{1}, {1, 2}, {1, 2, 3}, {1, 2, 3, 4}, {1, 2, 3, 4, 5}}

(* Transpose the list *)
list ~Flatten~ {2}
Out[2]= {{1, 1, 1, 1, 1}, {2, 2, 2, 2}, {3, 3, 3}, {4, 4}, {5}}

For more information on how the second argument of Flatten works and what it can do, see this answer by Leonid.

share|improve this answer
6  
Damn, I wanted to write that. Anyway, I've asked what Flatten's second argument does in a previous question and got a pretty nice answer, you can find it here: mathematica.stackexchange.com/questions/119/… –  David Mar 28 '12 at 19:19
1  
Wow Flatten is powerful –  Mr. Demetrius Michael Mar 29 '12 at 14:58
add comment

Just to show that there are always a zillion ways to do things in Mathematica, here is my version. Actually, I myself would have used Flatten and its mind-shattering second argument after having learned of its existence a couple of months ago.

Contrary to the Flatten method this one is straightforward and easy to understand, but boorish and probably not very efficient.

I'll start with a slightly modified test table to better demonstrate the results:

t = Drop[Table[q[i, #], {i, 10}], #] & /@ Range[10]) // TableForm

Mathematica graphics

The ragged array is changed to a rectangular using PadRight, padding it with a unique symbol generated by Unique. I then Transpose the matrix and remove the unique symbol by replacing it with Sequence (think of it as a black hole).

u = Unique[];
m = Length /@ t // Max;
Transpose[PadRight[#, m, u] & /@ t] /. u -> Sequence[] // TableForm

Mathematica graphics


This is probably how Mr.Wizard does it:

Module[{u}, Transpose[PadRight[t, Automatic, u]] /. u -> Sequence[]]

Same result, but compacter code.

share|improve this answer
3  
horrendous. +1 (for having the nerve to publicly display this) –  acl Mar 28 '12 at 20:59
1  
@acl and proud of it ;) –  Sjoerd C. de Vries Mar 28 '12 at 21:21
1  
I wrote the same thing years ago, but used Module instead of Unique and Automatic for m. –  Mr.Wizard Mar 29 '12 at 8:19
    
mr.wizard Should have thought of Automatic in PadRight –  Sjoerd C. de Vries Mar 29 '12 at 13:02
1  
Instead of the rigamarole with Module[], one could use Null as the placeholder: Transpose[PadRight[{{1}, {1, 2}, {1, 2, 3}, {1, 2, 3, 4}, {1, 2, 3, 4, 5}}, Automatic, Null]] /. Null -> Sequence[]. –  J. M. Jul 5 '12 at 5:11
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.