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.

I have a ragged list

ragged = {{a,b,c,d,e},{x,y,z}}

that I would like to trim (on the right) to be rectangular. The desired result is

{{a,b,c},{x,y,z}}

It seems like this should be a one-word operation but I can't find the one word. So far the best I can do is to PadRight with Null and then delete everything with a Null in it.

Transpose[
 DeleteCases[
  Transpose[
   PadRight[
    ragged,
    Automatic,
    Null
    ]
   ],
  a_ /; MemberQ[a, Null]
  ]
 ]

This seems way too complicated for such a simple operation. What is the simplest way to do this?

share|improve this question

4 Answers 4

up vote 12 down vote accepted

You are looking for Take. To then make the array rectangular, I would do something like this

Take[ragged, All, Min[Length /@ ragged] ]
share|improve this answer
    
How did he fix that without it showing the edit? –  Brett Champion Mar 20 '12 at 15:30
1  
@BrettChampion I was within the 5 minute window. –  rcollyer Mar 20 '12 at 15:31

Here is a timing comparison of the methods presented. I had to modify Heike's code because it did not work on version 7.

SetAttributes[timeAvg, HoldFirst]

timeAvg[func_] := 
 Do[If[# > 0.3, Return[#/5^i]] & @@ Timing@Do[func, {5^i}], {i, 0, 15}]

ragged = RandomReal[99, #] & /@ RandomInteger[{25, 100}, 5000];

PadRight[ragged, {Length@ragged, Min[Length /@ ragged]}]; // timeAvg

ragged[[All, ;; Min[Length /@ ragged]]]; // timeAvg

Take[ragged, All, Min[Length /@ ragged]]; // timeAvg
0.02808

0.002864

0.002744
ragged = RandomReal[99, #] & /@ RandomInteger[{2500, 5000}, 50];

PadRight[ragged, {Length@ragged, Min[Length /@ ragged]}]; // timeAvg

ragged[[All, ;; Min[Length /@ ragged]]]; // timeAvg

Take[ragged, All, Min[Length /@ ragged]]; // timeAvg
0.01688

0.00009184

0.000088896

rcollyer wins!

share|improve this answer
    
I modified my answer to what you proposed when I was fixed the "Trim" issue. –  rcollyer Mar 20 '12 at 15:43
    
Darn. You win again, Mr. Wizard (or rcollyer, I a afraid of a grandfather paradox). –  Yves Klett Mar 20 '12 at 15:49
    
@rcollyer corrected –  Mr.Wizard Mar 20 '12 at 15:49
2  
@YvesKlett well, we've already established that I'm part of the Mr.Wizard collective. –  rcollyer Mar 20 '12 at 15:58
1  
Great. Now I´ll have to spend the rest of the day to search for some far-fetched example to demonstrate Parts superiority. S´ not like I have a job or something. No borg, me! –  Yves Klett Mar 20 '12 at 15:59

What about:

ragged[[All, 1 ;; Min[Length /@ ragged]]]

{{a, b, c}, {x, y, z}}

share|improve this answer
2  
Very nice ! You can even drop 1 and write ragged[[All, ;; Min[Length /@ ragged]]] –  Artes Mar 20 '12 at 15:41
1  
Yep! Good for the oneliner contest but potentially dangerous for sloppy readers (i.e. me). –  Yves Klett Mar 20 '12 at 15:45

You could do something like

PadRight[ragged, {Automatic, Min[Length /@ ragged]}]
share|improve this answer
    
Automatic apparently doesn't work on v7. –  Mr.Wizard Mar 20 '12 at 15:31
3  
+1 for showing that PadRight can remove elements. I didn't know that. –  ArgentoSapiens Mar 20 '12 at 16:49

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.