MathGroup Archive 2000

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

Search the Archive

Re: List element manipulation

  • To: mathgroup at smc.vnet.net
  • Subject: [mg25496] Re: List element manipulation
  • From: adam_smith at my-deja.com
  • Date: Thu, 5 Oct 2000 23:50:16 -0400 (EDT)
  • References: <8r19qf$hvt@smc.vnet.net> <R9UB5.94602$Zh6.213678@ralph.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

I thought that you might find this of interest.  I decided to try a
different method using ReplacePart[].  As shown below it can run quite
a bit faster for large lists.  IMHO It also seems a little clearer to
follow, although not as compact and elegant as MapAt[].  Note, the
timing differences are insignificant for vectors 2000 elements or less.
(I am running Version 4.0.1.0 under Windows NT SP5 I think on a 333 MHz
Pentium II with 196 MB memory)

In[30]:=
v = Table[n, {n, 1, 200000}];

In[41]:=
Timing[newv = MapAt[# - 1 &, v, -1];]
newv[[300]]
newv[[Length[newv]]]

Out[41]=
{0.37 Second, Null}

Out[42]=
300

Out[43]=
199999

In[44]:=
Timing[mynewv = ReplacePart[v, v[[Length[v]]] - 1, Length[v]];]
mynewv[[300]]
mynewv[[Length[mynewv]]]

Out[44]=
{0.01 Second, Null}

Out[45]=
300

Out[46]=
199999


In article <R9UB5.94602$Zh6.213678 at ralph.vnet.net>,
  "Allan Hayes" <hay at haystack.demon.co.uk> wrote:
> Martin:
>
> MapAt[# - 1 &, {1, 2, 3}, -1]
>
>         {1, 2, 2}
>
> Allan
> ---------------------
> Allan Hayes
> Mathematica Training and Consulting
> Leicester UK
> www.haystack.demon.co.uk
> hay at haystack.demon.co.uk
> Voice: +44 (0)116 271 4198
> Fax: +44 (0)870 164 0565
>
> "Martin Rommel" <Martin_Rommel at mac.com> wrote in message
> news:8r19qf$hvt at smc.vnet.net...
> > I want to decrement the last number in list. This ought to be easy,
but my
> > initial attempts did not bear fruits:
> >
> > In[40]:=
> > MapAt[Decrement, Range[116, 166, 8], -1]
> >
> >     Decrement::"rvalue": "164 is not a variable with a value, so
its value
> > cannot be changed."
> >
> > Out[40]=
> > {116, 124, 132, 140, 148, 156, 164--}
> >
> > Do I need to use ReplacePart? That works but still gives me an
error!
> >
> > In[53]:=
> > Range[116, 166, 8] // ReplacePart[#, --Last[#], -1] &
> >
> >     Set::"write": "Tag Last in Last[{116, 124, 132, 140, 148, 156,
164}]
> is
> > Protected."
> >
> > Out[53]=
> > {116, 124, 132, 140, 148, 156, 163}
> >
> > Different error here:
> >
> > In[51]:=
> > Range[116, 166, 8] // ReplacePart[#, --#[[-1]], -1] &
> >
> >     Set::"setps": "{116, 124, 132, 140, 148, 156, 164} in
assignment of
> part
> > is not a symbol."
> >
> > Out[51]=
> > {116, 124, 132, 140, 148, 156, 163}
> >
> >
> > Anybody out there able to enlighten me?
> >
> > Thanks, Martin
> >
> >
> >
>
>


Sent via Deja.com http://www.deja.com/
Before you buy.


  • Prev by Date: RE: Hold, HoldForm, ReleaseHold when Plotting multiple functions
  • Next by Date: ISSAC 2001 - Call for Papers
  • Previous by thread: Re: List element manipulation
  • Next by thread: Re: List element manipulation