Re: Better Way of Testing and Replacing List Elements?
- To: mathgroup at smc.vnet.net
- Subject: [mg103972] Re: [mg103949] Better Way of Testing and Replacing List Elements?
- From: Leonid Shifrin <lshifr at gmail.com>
- Date: Wed, 14 Oct 2009 07:55:43 -0400 (EDT)
- References: <200910140319.XAA22469@smc.vnet.net>
Hi, You may skip Mapping since Negative is listable - this may give you some speedup: ReplacePart[xxx, Position[Negative@xxx, True] -> 0] If your list is large, ReplacePart may slow down. Even faster is xxx /. _?Negative :> 0 Still, the best and fastest way to do it is probably this: Clip[xxx, {0, Infinity}] Regards, Leonid On Wed, Oct 14, 2009 at 7:19 AM, careysub <careysub at gmail.com> wrote: > The code below replaces the negative values in a list with zero, and > is an example of a type of operation I use a lot: > > xxx = {1, 2, 3, 4, 5, -6, -7, 8, 9, 10, -1, 11, 12}; > ReplacePart[xxx, Position[Map[Negative, xxx], True] -> 0] > > Is there a "better" way of doing this (fewer function calls, more > efficient)? > > My feeling is that I'm doing this in an awkward way. > >
- References:
- Better Way of Testing and Replacing List Elements?
- From: careysub <careysub@gmail.com>
- Better Way of Testing and Replacing List Elements?