MathGroup Archive 2008

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Drop elements from list

  • To: mathgroup at smc.vnet.net
  • Subject: [mg94306] Re: Drop elements from list
  • From: Ray Koopman <koopman at sfu.ca>
  • Date: Wed, 10 Dec 2008 04:45:55 -0500 (EST)
  • References: <ghdmqj$flr$1@smc.vnet.net> <ghlme9$k26$1@smc.vnet.net>

On Dec 9, 3:56 am, SigmundV <sigmu... at gmail.com> wrote:
> Thank you for all the solutions. Some of them I should have come up
> with myself. My favorite is probably Fold[Drop[##]&,a,{{7,10},{2,5}}].
> I don't know whether it is the fastest or not, but it is easy to
> program (as in "characters to type"). Ray Koopman's solution with
> Delete is also good (although I would like to point out that
> Transpose@Flatten@{Range[2,5],Range[7,10]} does not work as it is, but

I omitted a pair of curly brackets. I meant to write
  Transpose@{Flatten@{Range[2,5],Range[7,10]}}.

> Partition[...,1] does the trick) and easy to program as well. The idea
> presented by Raffy and dimitris, to pick the relevant elements instead
> of deleting, is also good. I have discovered that it generally is
> faster to pick explicit elements from a list than to use functions as
> Drop or Delete. In that respect I like Raffy's solution, but it seems
> that too much time is spent in Complement.
>
> Finally, below is a cell with timings of the different solutions. I
> would like to hear your comments on this. The final line is included
> to see how much time is spent in Complement.
>
> a = RandomReal[{}, 2000000];
> Delete[a, Partition[Range[2, 5]~Join~Range[7, 10]~Join~Range[15, 18],
> 1]]; // Timing
> a[[{1, 6}~Join~Range[11, 14]~Join~Range[19, Length[a]]]]; // Timing
> Fold[Drop[##] &, a, {{15, 18}, {7, 10}, {2, 5}}]; // Timing
> a[[Complement[Range[Length[a]], Range[2, 5], Range[7, 10], Range[15,
> 18]]]]; // Timing
> Complement[Range[Length[a]], Range[2, 5], Range[7, 10], Range[15,
> 18]]; // Timing

a = RandomReal[{}, 2*^7];
b1 = Delete[a, Partition[Join[Range[2,5],Range[7,10],
                              Range[15,18]],1]]; // Timing
b2 = a[[Join[{1,6},Range[11,14],Range[19,Length@a]]]]; // Timing
b3 = Fold[Drop[##]&, a, {{15,18},{7,10},{2,5}}]; // Timing
b4 = a[[Complement[Range@Length@a, Range[2,5],Range[7,10],
                                   Range[15,18]]]]; // Timing
b5 = a[[komplement[Length@a, Join[Range[2,5],Range[7,10],
                                  Range[15,18]]]]]; // Timing
SameQ[b1,b2,b3,b4,b5]
c1 = Complement[Range@Length@a, Range[2,5],Range[7,10],
                                Range[15,18]]; // Timing
c2 = komplement[Length@a, Join[Range[2,5],Range[7,10],
                                Range[15,18]]]; // Timing
SameQ[c1,c2]

{0.55,Null}
{1.88,Null}
{1.81,Null}
{8.91,Null}
{3.65,Null}
True
{7.81,Null}
{2.,Null}
True

If your lists will usually be so long, with so few elements to
be deleted, it looks like deleting will be faster than taking.

>
> /Sigmund
>
> On Dec 6, 12:14 pm, SigmundV <sigmu... at gmail.com> wrote:
>
>> Dear group,
>>
>> Consider a list of length, say, 20:
>>
>> a = RandomReal[{},20];
>>
>> How would you then drop/delete elements, say, 2-5 and 7-10 from this
>> list? The built-in function Drop does not seem to include such a
>> possibility. I should add that I do not have access to Mathematica 7,
>> so any solution should work in version 6.
>>
>> Kind regards,
>> Sigmund


  • Prev by Date: Re: Mathematica 7 is now available
  • Next by Date: Beta[x,a,b]: how to get around bug
  • Previous by thread: Re: Drop elements from list
  • Next by thread: Processing large data sets