Re: Better Way of Testing and Replacing List Elements?
- To: mathgroup at smc.vnet.net
- Subject: [mg103979] Re: [mg103949] Better Way of Testing and Replacing List Elements?
- From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
- Date: Wed, 14 Oct 2009 07:57:00 -0400 (EDT)
- References: <200910140319.XAA22469@smc.vnet.net>
On 14 Oct 2009, at 12:19, 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.
>
For example:
xxx /. _?Negative -> 0
{1,2,3,4,5,0,0,8,9,10,0,11,12}
or
xxx*UnitStep[xxx]
{1,2,3,4,5,0,0,8,9,10,0,11,12}
Andrzej Kozlowski
- References:
- Better Way of Testing and Replacing List Elements?
- From: careysub <careysub@gmail.com>
- Better Way of Testing and Replacing List Elements?