MathGroup Archive 2008

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

Search the Archive

Re: Drop elements from list

  • To: mathgroup at smc.vnet.net
  • Subject: [mg94309] Re: Drop elements from list
  • From: Raffy <raffy at mac.com>
  • Date: Wed, 10 Dec 2008 04:46:28 -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
> 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
>
> /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

It's not really a fair comparison, as only the Complement can handle
any combination of ranges, even malformed ranges.
Delete/Partition solution comes close, but still will fail if you give
it malformed range.
The other solutions suggested require that the ranges are non-
overlapping, reverse sorted, and well-formed.

To have the benefit of being able to give the Fold/Drop solution with
any combination of well-formed ranges, you could do:

ranges = { Range[2,5], Range[7,10], Range[15, 18] };

Fold[Drop, a, Reverse[Split[Union @@ ranges, #2 - #1 == 1 &][[All, {1,
-1}]]]]

ie. this would let you do something like: { Range[2, 10], Range[3,
11], Range[15,18] };


  • Prev by Date: Re: Re: A 3D Plot Query
  • Next by Date: Re: Re: Plot vs NMaximize
  • Previous by thread: Re: Drop elements from list
  • Next by thread: Re: Drop elements from list