Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2009

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

Search the Archive

Re: Better Way of Testing and Replacing List Elements?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg104006] Re: Better Way of Testing and Replacing List Elements?
  • From: Bill Rowe <readnews at sbcglobal.net>
  • Date: Thu, 15 Oct 2009 07:17:36 -0400 (EDT)

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.



  • Prev by Date: Re: trading
  • Next by Date: Re: Grassmann algebra package
  • Previous by thread: Re: Better Way of Testing and Replacing List Elements?
  • Next by thread: Re: Better Way of Testing and Replacing List Elements?