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: [mg103992] Re: [mg103949] Better Way of Testing and Replacing List Elements?
  • From: "Richard Hofler" <rhofler at bus.ucf.edu>
  • Date: Wed, 14 Oct 2009 08:05:55 -0400 (EDT)
  • References: <200910140319.XAA22469@smc.vnet.net>

Hello,

I'm betting that you'll see many ways to do this. So, here's one:

In[21]:= lst={1,2,3,4,5,-6,-7,8,9,10,-1,11,12};

Replace[lst,x_/;Negative[x]->0,1]

Out[22]= {1,2,3,4,5,0,0,8,9,10,0,11,12}


Richard Hofler

________________________________

From: careysub [mailto:careysub at gmail.com]
Sent: Tue 10/13/2009 11:19 PM
To: mathgroup at smc.vnet.net
Subject: [mg103992] [mg103949] Better Way of Testing and Replacing List Elements?



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.




  • Prev by Date: Mathematica 5.2 with xorg-server-1.6 and updated Xorg libraries
  • 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?