Re: Drop elements from list
- To: mathgroup at smc.vnet.net
- Subject: [mg94251] Re: Drop elements from list
- From: SigmundV <sigmundv at gmail.com>
- Date: Tue, 9 Dec 2008 06:57:10 -0500 (EST)
- References: <ghdmqj$flr$1@smc.vnet.net>
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