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.

As the titel says I want to transpose a matrix containing rows of unequal length, to be able to transport them to excel (in row form, there are too many datapoint to write them in a cell. In another topic if found to use flatten : Transpose uneven lists

However, this does not give me the right result:

Example:

list = {{1, 2, 3, 4}, {1, 2, 3, 4}, {1, 2, 3}, {1, 2, 3}, {1, 2, 3, 4,5}, {1, 2, 3, 4, 5}};
Flatten[list, {{2},{1}}]

Results into:

{{1, 1, 1, 1, 1, 1}, {2, 2, 2, 2, 2, 2}, {3, 3, 3, 3, 3, 3}, {4, 4, 4, 4}, {5, 5}}

But the result I want is:

{{1, 1, 1, 1, 1, 1}, {2, 2, 2, 2, 2, 2}, {3, 3, 3, 3, 3, 3}, {4, 4,"" , "" , 4, 4}{,"" , "" , "" , "" , 5, 5}}

So I want to keep the rows together as they are, but only print them into columns. Is this possible?

share|improve this question
3  
What is the "rule" that puts the 4 in pairs front and back, yet the 5 is only at the back? –  rasher Feb 24 at 11:10

3 Answers 3

Is this what you want?

list = {{1, 2, 3, 4}, {1, 2, 3, 4}, {1, 2, 3}, {1, 2, 3},
        {1, 2, 3, 4, 5}, {1, 2, 3, 4, 5}};

Transpose@PadRight[list, Automatic, ""]

{{1, 1, 1, 1, 1, 1}, {2, 2, 2, 2, 2, 2}, {3, 3, 3, 3, 3, 3},
 {4, 4, "", "", 4, 4}, {"", "", "", "", 5, 5}}
share|improve this answer
    
your answer is most compact...always learning Automatic...got my vote despite nose in front in a three horse race –  ubpdqn Feb 24 at 11:59

If the aim is for export into spreadsheet you could use:

With[{m = Max[Length /@ list]},Transpose@(PadRight[#, m, ""] & /@ list)]

Visualizing result to see if this is your aim:

enter image description here

share|improve this answer
2  
And the photo finish.... @ubpdqn! Or so it showed up on my screen. :P –  Michael E2 Feb 24 at 11:15
    
@MichaelE2 lol! now if only it answers OP's question –  ubpdqn Feb 24 at 11:17
    
Great! All three results do what I want! –  Wiebe Feb 24 at 11:53
maxLen = Max@(Length /@ list)

Transpose[PadRight[#, maxLen, ""] & /@ list]
share|improve this answer

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.