Re: Better Way of Testing and Replacing List Elements?
- To: mathgroup at smc.vnet.net
- Subject: [mg104039] Re: Better Way of Testing and Replacing List Elements?
- From: "E. Martin-Serrano" <eMartinSerrano at telefonica.net>
- Date: Fri, 16 Oct 2009 07:21:14 -0400 (EDT)
Hi, After several tries the faster seems to be xxx*UnitStep[xxx] See: In[1]:= xxx = RandomChoice[{-1, 1}, 1000000]; In[2]:= Length@Select[xxx, (# == -1) &] Out[2]= 500002 In[3]:= Timing[ ReplacePart[xxx, Position[Map[Negative, xxx], True] -> 0];] Out[3]= {2.672, Null} In[4]:= Timing[(xxx /. _?Negative -> 0);] Out[4]= {0.796, Null} In[5]:= Timing[(xxx*UnitStep[xxx]);] Out[5]= {0.063, Null} In[6]:= Timing[Clip[xxx, {0, Infinity}];] Out[6]= {0.171, Null} In[7]:= Timing[(Max[#, 0] & /@ xxx);] Out[7]= {1.406, Null} In[8]:= Timing[(xxx /. (x_ /; x < 0) -> 0);] Out[8]= {0.859, Null} In[9]:= Timing[(xxx Unitize[Sign[xxx] + 1]);] Out[9]= {0.078, Null} Emilio -----Original Message----- From: Bill Rowe [mailto:readnews at sbcglobal.net] Sent: Thursday, October 15, 2009 12:18 PM To: mathgroup at smc.vnet.net Subject: [mg104039] [mg104006] Re: Better Way of Testing and Replacing List Elements? On 10/13/09 at 11:19 PM, careysub at gmail.com (careysub) 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. Other ways would be: In[1]:= xxx = {1, 2, 3, 4, 5, -6, -7, 8, 9, 10, -1, 11, 12}; In[2]:= Clip[xxx, {0, Infinity}] Out[2]= {1,2,3,4,5,0,0,8,9,10,0,11,12} In[3]:= xxx /. a_?Negative :> 0 Out[3]= {1,2,3,4,5,0,0,8,9,10,0,11,12} In[4]:= xxx Unitize[Sign[xxx] + 1] Out[4]= {1,2,3,4,5,0,0,8,9,10,0,11,12} =46or small data sets, the timing of these won't be much different. For large data sets, Clip is probably the fastest.
- Follow-Ups:
- Re: Better Way of Testing and Replacing List Elements?
- From: Carl Woll <carlw@wolfram.com>
- Re: Better Way of Testing and Replacing List Elements?