MathGroup Archive 2000

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

Search the Archive

Re: Mapping down two lists

  • To: mathgroup at smc.vnet.net
  • Subject: [mg25211] Re: [mg25179] Mapping down two lists
  • From: Matt.Johnson at autolivasp.com
  • Date: Fri, 15 Sep 2000 02:21:47 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

John-

In[21]:=
lst1 = Table[Random[], {500000}];
lst2 = Table[{Random[], Random[]}, {500000}];
In[23]:=
Plus @@ (Transpose[lst2][[1]] - lst1)^2) // Timing
Out[23]=
{1.87 Second, 83259.7}
In[24]:=
Apply[Plus, (#[[1]] & /@ lst2 - lst1)^2] // Timing
Out[24]=
{2.91 Second, 83259.7}

-matt





"John Satherley" <js1 at liverpool.ac.uk> on 09/12/2000 07:24:23 PM

cc:

Subject: [mg25211]  [mg25179] Mapping down two lists


Hi MathGroup
I'm trying to sharpen up my functional programming skills and have come
across a problem which is probably very trivial but I can't find a
satisfactory solution.

I have two lists of equal length, one 2 dimensional and the other 1
dimensional:
lst1={{1,2},{2,3},{1,3},......}
lst2={1,2,2,3,4,5.....}

What I want to do is find the difference between lst1[[i,1]] and lst2[[i]]
square it and then sum up all the terms over the length of the list.

It is easy to do this in terms of Tables but I'm trying to find a fast
solution for long lists that uses map and other functional programming
tools.

The best I have come up with is:
Apply[Plus,([#[[1]]&/@lst1-lst2)^2]
but I'm wondering if it is possible to map directly onto the two lists
without first having to extract the elements of the first list? Map seems
to
only work down a single list.

I would be most grateful of any hints as to how this might be performed in
an efficient manner.

Thanks for help
Dr. John Satherley
Dept of Chem
University of Liverpool
Liverpool L69 7ZD
UK
js1 at liv.ac.uk










  • Prev by Date: Re: Random spherical troubles
  • Next by Date: ParametricPlot3D is buggy
  • Previous by thread: Re: Mapping down two lists
  • Next by thread: Re: Mapping down two lists