MathGroup Archive 2010

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

Search the Archive

Re: Re: Replace list element based on a condition how to

  • To: mathgroup at smc.vnet.net
  • Subject: [mg106692] Re: [mg106652] Re: [mg106630] Replace list element based on a condition how to
  • From: Canopus56 <canopus56 at yahoo.com>
  • Date: Thu, 21 Jan 2010 04:53:08 -0500 (EST)
  • References: <201001191013.FAA29062@smc.vnet.net> <4B55E3E4.1020804@wolfram.com> <201001201148.GAA09160@smc.vnet.net> <34c814851001200525j773e7efweaf8c7ceb03e9eae@mail.gmail.com>

Thanks, Leonard.  That is an interesting performance difference.

> In[18]:= lst1 /. n_Integer :> fncHourFix[n]

Another newbie type question.  Several responders recommended: 

> In[18]:= lst1 /. n_Integer :> fncHourFix[n]

Q1.  What is the "_Integer" part of "n_Integer"? 

Is that just declaring a variable name "n_Integer" or is that a way to declare a datatype "Integer".

If that syntax is a way to declare a datatype, where is that referenced in Mathematica documentation?  I really scoured the Documentation trying to find how to force a datatype, e.g. - VBA's equivalent to CStr, CDbl, CInt, etc. 

Thanks - Kurt 

________________________________
From: Leonid Shifrin <lshifr at gmail.com>
To: Canopus56 <canopus56 at yahoo.com>; mathgroup at smc.vnet.net
Sent: Wed, January 20, 2010 6:25:42 AM
Subject: [mg106692] Re: [mg106652] Re: [mg106630] Replace list element based on a condition how to

Hi Kurt, 

just in case you have really large datasets to work on, the rule-based solution is not the most efficient. Here is another one, which worked on my tests about 40 times faster:

Clear[fncHourFixAlt];
fncHourFixAlt[
   hours_List] := ((hours - 6)*# + (hours - 6 + 24)*(1 - #)) &@
   UnitStep[hours - 6];


In[17]:= lst1 = {18, 19, 20, 21, 22, 23, 0, 1, 2, 3}

Out[17]= {18, 19, 20, 21, 22, 23, 0, 1, 2, 3}

In[18]:= lst1 /. n_Integer :> fncHourFix[n]

Out[18]= {12, 13, 14, 15, 16, 17, 18, 19, 20, 21}

In[19]:= fncHourFixAlt@lst1

Out[19]= {12, 13, 14, 15, 16, 17, 18, 19, 20, 21}

In[20]:= largelst = RandomInteger[{0, 23}, 500000];

In[21]:= (res1 = largelst /. n_Integer :> fncHourFix[n]); // Timing

Out[21]= {1.25, Null}

In[25]:= (res2 = fncHourFixAlt[largelst]); // Timing

Out[25]= {0.031, Null}

In[23]:= res1 === res2

Out[23]= True

Regards,
Leonid




On Wed, Jan 20, 2010 at 2:48 PM, Canopus56 <canopus56 at yahoo.com> wrote:

Thanks to all for the many replies in this thread.  All the replies has me on the right track. - Kurt
>
>



      


  • Prev by Date: Re: exporting numerical data
  • Next by Date: Re: Sorting paired columns of dates and values
  • Previous by thread: Re: Replace list element based on a condition how to
  • Next by thread: Re: Replace list element based on a condition how to