MathGroup Archive 2008

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

Search the Archive

Re: Apply function to parts of a list

  • To: mathgroup at smc.vnet.net
  • Subject: [mg86260] Re: [mg86224] Apply function to parts of a list
  • From: Stern <nycstern at gmail.com>
  • Date: Fri, 7 Mar 2008 02:27:10 -0500 (EST)
  • References: <200803060759.CAA29253@smc.vnet.net>

<I sent this reply to guerom00 directly but am resending it to group so it
gets archived for posterity>

On Thu, Mar 6, 2008 at 8:43 AM, Stern <nycstern at gmail.com> wrote:

> There are probably a lot of ways to do this. The most natural for me is to
> use nested Transpose commands; this has the advantage of avoiding anonymous
> functions, so it's clearer if nested in the middle of other commands that
> use anonymous functions.
> my way:
>
> Transpose[{2*Transpose[data][[1]], Transpose[data][[2]]}]
>
>
> This may not be memory- or compute-efficient, as it requires repeating the
> Transpose[data] command, so for large datasets you may prefer
>
>
> Module[{transp}, transp = Transpose[data];
>   Transpose[{2*transp[[1]], transp[[2]]}]];
>
>
> Either of these is quicker than where you started --
>
>
> In[19]:= Timing[MapAt[2*# &, data, Table[{i, 1}, {i, 1, Length[data]}]];]
> Out[19]= {0.004409, Null}
>
>
> In[20]:= Timing[Transpose[{2*Transpose[data][[1]],
> Transpose[data][[2]]}];]
> Out[20]= {0.000672, Null}
>
>
> In[21]:= Timing[Module[{transp}, transp = Transpose[data];
>    Transpose[{2*transp[[1]], transp[[2]]}]];]
> Out[21]= {0.000698, Null}
>
>
> Cheers,
>
>
> Michael
>
>
>
> On Thu, Mar 6, 2008 at 2:59 AM, guerom00 <guerom00 at gmail.com> wrote:
>
> > Hi all,
> >
> > Very often, I find myself having datas to plot of this form :
> >
> > data={{x1,y1},{x2,y2},{x3,y3}...{xn,yn}}
> >
> > So, very conventional : a list of abscissa and ordinate. Now, let's say
> > I want to multiply by 2 all my ascissa. Right now, I write :
> >
> > MapAt[2*#&,data,Table[{i,1},{i,1,Length[data]}]]
> >
> > It's OK but I isn't there another way to achieve this as I find this
> > rather "involved" for such a simple thing ?
> >
> > Thanks in advance.
> >
> >
>


  • Prev by Date: Re: Bold Script Characters in 6.0.2
  • Next by Date: Re: Writing a HoldAll function that evaluates its arguments
  • Previous by thread: Re: Apply function to parts of a list
  • Next by thread: Re: Re: Apply function to parts of a list