MathGroup Archive 2007

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

Search the Archive

Re: Re: Setting Negatives to Zero

  • To: mathgroup at smc.vnet.net
  • Subject: [mg82839] Re: [mg82769] Re: Setting Negatives to Zero
  • From: "Kevin J. McCann" <Kevin.McCann at umbc.edu>
  • Date: Thu, 1 Nov 2007 05:10:30 -0500 (EST)

Thanks Andrzej,

Here is what I tried:

x =.
xx =.
yy =.
g = Compile[{{x, _Real, 2}}, x*UnitStep[x]];
Timing[x = RandomReal[{-0.2, 3}, {60000, 600}];]
Timing[xx = g[x];]
Timing[yy = Clip[x, {0, \[Infinity]}];]
xx == yy

With outputs:

{1.89,Null}
{2.094,Null}
{0.406,Null}
True

It appears that Clip[] is very efficient.

Kevin

Andrzej Kozlowski wrote:

You can do it somewhat faster by not using patterns and compiling. For 
example, if your data set is a list of reals:

f = Compile[{{x, _Real, 1}}, x*UnitStep[x]]

should be faster than the codes below (at least it is so on my 
PowerBook). Of course you have to change it to:

g = Compile[{{x, _Real, 2}}, x*UnitStep[x]]

if your data set is a matrix.

Andrzej Kozlowski


On 30 Oct 2007, at 19:46, Steve Luttrell wrote:

> These do the sort of thing you want
>
> {1, 2, -3, -4, 5, 6} /. {_?(# < 0 &) -> 0}
>
> {1, 2, -3, -4, 5, 6} /. {_?NonPositive -> 0}
>
> and both give
>
>
> -- 
> Steve Luttrell
> West Malvern, UK
>
> "Kevin J. McCann" <Kevin.McCann at umbc.edu> wrote in message
> news:fg6qha$dj0$1 at smc.vnet.net...
>> I have a very large data set (64000 x 583) in which negative values
>> indicate "no data", unfortunately these negatives are not all the same.
>> I would like to efficiently set all these negatives to zero. I know that
>> I will likely be embarrassed when I see how to do it, but I can't seem
>> to remember or figure it out. I should emphasize that because of the
>> size of the data set, this needs to be done efficiently. Another
>> programming language does it as follows:
>>
>> x(x < 0) = 0;
>>
>> Thanks,
>>
>> Kevin
>> -- 
>>
>> Kevin J. McCann
>> Research Associate Professor
>> JCET/Physics
>> Physics Building
>> University of Maryland, Baltimore County
>> 1000 Hilltop Circle
>> Baltimore, MD 21250
>>
>
>


-- 

Kevin J. McCann
Research Associate Professor
JCET/Physics
Physics Building
University of Maryland, Baltimore County
1000 Hilltop Circle
Baltimore, MD 21250



  • Prev by Date: Re: Frequency response function
  • Next by Date: Re: Re: Setting Negatives to Zero
  • Previous by thread: Re: Re: Setting Negatives to Zero
  • Next by thread: Re: Re: Setting Negatives to Zero