Re: Building a list
- To: mathgroup at smc.vnet.net
- Subject: [mg47128] Re: [mg47114] Building a list
- From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
- Date: Sat, 27 Mar 2004 01:34:31 -0500 (EST)
- References: <200403260856.DAA22958@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
On 26 Mar 2004, at 08:56, Mark Coleman wrote:
> Greetings,
>
> I've got a relatively simple function f[a_,b_,c_,d_], where a and b are
> integers and c and d are lists (of reals), and f returns a real.
>
> I need to evaluate f on (potentially long) sets of lists and calculate
> a square "matrix" from the possible permutations of the elements in the
> lists. For instance
>
> If
>
> xlist={x1,x2,x3}
>
>
> I need to calculate
>
> {{f[a,b,x1,x1],f[a,b,x1,x2],f[a,b,x1,x3]},{f[a,b,x2,x1],f[a,b,x2,x2],f[
> a
> ,b,x2,x3]},{f[a,b,x3,x1],f[a,b,x3,x2],f[a,b,x3,x3]}}
>
> Note that f[a,b,c,d] does not equal f[a,b,d,c].
>
> My first reaction was that Outer[] might do this, but I cannot seem to
> get it to work. I'd appreciate any suggestions.
>
> Thanks,
>
> Mark
>
>
>
You were quite right, Outer is certainly a natural function to use here:
xlist={x1,x2,x3}
Apply[f[a,b,##1]&,Outer[List,xlist,xlist],{2}]
{{f[a,b,x1,x1],f[a,b,x1,x2],f[a,b,x1,x3]},{f[a,b,x2,x1],
f[a,b,x2,x2],f[a,b,x2,x3]},{f[a,b,x3,x1],f[a,b,x3,x2],f[a,b,x3,x3]}}
Andrzej Kozlowski
Chiba, Japan
http://www.mimuw.edu.pl/~akoz/
- References:
- Building a list
- From: Mark Coleman <mark@markscoleman.com>
- Building a list