RE: Applying my function to elements of a vector
- To: mathgroup at smc.vnet.net
- Subject: [mg49492] RE: [mg49464] Applying my function to elements of a vector
- From: "David Park" <djmp at earthlink.net>
- Date: Thu, 22 Jul 2004 02:44:59 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
Gregory,
The easiest method to do this on the fly is to Map f onto the list.
f /@ {a, b, c, d}
{f[a], f[b], f[c], f[d]}
Or you could make f Listable just as most Mathematica functions are.
Attributes[f] = {Listable};
f[{a, b, c, d}]
{f[a], f[b], f[c], f[d]}
David Park
djmp at earthlink.net
http://home.earthlink.net/~djmp/
From: Gregory Lypny [mailto:gregory.lypny at videotron.ca]
To: mathgroup at smc.vnet.net
Hello everyone,
Say I've created a function, f[x_], which accepts a single scalar as
its argument. I now want to apply that function to each element of a
vector, v, of length n, as in
newVector = f[v],
but I get an error because my function f does not thread over the
elements of the vector as do built-in Mathematica functions such as
Log[]. How can I get my function to apply to each element of my
vector?
Regards,
Greg