RE: a little more complex list operation
- To: mathgroup at smc.vnet.net
- Subject: [mg40256] RE: [mg40237] a little more complex list operation
- From: "David Park" <djmp at earthlink.net>
- Date: Fri, 28 Mar 2003 04:31:09 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
Nathan, Mathematica is great at this stuff and if you expect to do a lot of it you should read up on Map and pure functions. data = {{x1, y1}, {x2, y2}, {x3, y3}}; {f[First[#]], g[Last[#]]} & /@ data {{f[x1], g[y1]}, {f[x2], g[y2]}, {f[x3], g[y3]}} Another method that has a pure function with another pure function inside. MapThread[#1[#2] &, {{f, g}, #}] & /@ data {{f[x1], g[y1]}, {f[x2], g[y2]}, {f[x3], g[y3]}} Here is an example with specific functions, squaring for f and Cos for g. We have more pure functions embedded. Remember that a pure function stands in the place of a name of a function. (MapThread[#1[#2] & , {{#1^2 & , Cos}, #1}] & ) /@ data {{x1^2, Cos[y1]}, {x2^2, Cos[y2]}, {x3^2, Cos[y3]}} Probably not as efficient for long data lists is data /. {x_, y_} :> {f[x], g[y]} {{f[x1], g[y1]}, {f[x2], g[y2]}, {f[x3], g[y3]}} I don't know exactly what you mean by "applying only one transform". It is probably not f[data] f[{{x1, y1}, {x2, y2}, {x3, y3}}] nor f /@ data {f[{x1, y1}], f[{x2, y2}], f[{x3, y3}]} but more likely Map[f, data, {2}] {{f[x1], f[y1]}, {f[x2], f[y2]}, {f[x3], f[y3]}} or f @@ # & /@ data {f[x1, y1], f[x2, y2], f[x3, y3]} You can see that there are many ways of manipulating data lists with Mathematica. The ability of functional programming to manipulate data structures is one of Mathematica's neatest features. David Park djmp at earthlink.net http://home.earthlink.net/~djmp/ From: Nathan Moore [mailto:nmoore at physics.umn.edu] To: mathgroup at smc.vnet.net Thanks very much for all the previous replies. Another simple question - though I guess this will require more complex syntax. Suppose I have the list, data = {{x1,y1},{x2,y2},{x3,y3}} and I want to produce the result (for fitting/plotting) data2 = {{f[x1],g[y1]},{f[x2],g[y2]},{f[x3],g[y3]}} I assume that if there was only one transform I wanted to apply, f[x], then I could say data2 = f[data] but how about this more complicated transform with functions f and g? again thanks! Your replies have been very helpful! Nathan Moore, University of Minnesota Physics