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: [mg103970] Re: Better Way of Testing and Replacing List Elements?
  • From: Ray Koopman <koopman at sfu.ca>
  • Date: Wed, 14 Oct 2009 07:55:21 -0400 (EDT)
  • References: <hb3fus$lsk$1@smc.vnet.net>

On Oct 13, 8:18 pm, careysub <carey... 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.

Here are three other ways to do it:

In[3]:= xxx*UnitStep[xxx]
Out[3]= {1,2,3,4,5,0,0,8,9,10,0,11,12}

In[4]:= Clip[xxx,{0,Infinity}]
Out[4]= {1,2,3,4,5,0,0,8,9,10,0,11,12}

In[5]:= xxx /. _?Negative -> 0
Out[5]= {1,2,3,4,5,0,0,8,9,10,0,11,12}


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