Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Possible Duplicate:
How do I use Map for a function with two arguments?

If I have a function:

F[x_,y_,z_]:=x*y*z

and I want to call it with several different x values using Map (and y and z fixed), is there a way to do that with a one-liner?

That is, for these x values

xVals = {1,2,3,4}

I want to somehow get:

{F[1,10,100], F[2,10,100], F[3,10,100],F[4,10,100]}

If I can do it with Map, it would be great, because I have many cores and want to speed this up with the parallelized Map.

share|improve this question

marked as duplicate by rm -rf Oct 9 '12 at 21:57

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2  
You may use map with a pure function, f[#,10,100]&/@ xvals. (I renamed your function to lowercase to keep with conventions.) –  David Carraher Oct 9 '12 at 19:56
    
+1 Thanks! Is there a way to do it in a grid (i.e. vary xvals and yvals)? –  Oliver Oct 9 '12 at 19:58
    
I want to essentially do a nested for loop to return a nested list: { forall yvals {forall xvals f[xval, yval,100]} } –  Oliver Oct 9 '12 at 20:02
    
I'm fairly certain this is a duplicate but I cannot find it. Anyone? –  Mr.Wizard Oct 9 '12 at 20:26
1  
@Mr.Wizard yes, in this case. Don't delete, though: the answers are good –  Verbeia Oct 9 '12 at 21:40

3 Answers 3

up vote 7 down vote accepted

You may use map with a pure function:

f[#,10,100]& /@ xVals
{f[1, 10, 100], f[2, 10, 100], f[3, 10, 100], f[4, 10, 100]}

Table will also work:

Table[f[x, 10, 100], {x, xVals}]
{f[1, 10, 100], f[2, 10, 100], f[3, 10, 100], f[4, 10, 100]}

Multiple iterator form:

Table[f[x, y, 100], {x, {1, 2, 3, 4}}, {y, {5, 6, 7, 8}}]
share|improve this answer
    
+1 I can also use parallel table to get an 8x speedup! –  Oliver Oct 9 '12 at 20:30
    
Thanks, Mr. Wizard, for the edits. –  David Carraher Oct 9 '12 at 22:04

You can use Outer to get a nested output list :

Outer[f[#1, #2, 100] &, {1, 2, 3, 4}, {5, 6, 7, 8}]
(* {{500, 600, 700, 800}, {1000, 1200, 1400, 1600}, 
   {1500, 1800, 2100, 2400}, {2000, 2400, 2800, 3200}} *)

However I'm not sure you can take advantage of Parallelize.

share|improve this answer
F[x_, y_, z_] := x*y*z;
xVals = {1, 2, 3, 4};
yVals = {5, 6, 7, 8};

You could use thread for this:

Thread[d[xVals, yVals, 100]]
(* yields *)
{d[1, 5, 100], d[2, 6, 100], d[3, 7, 100], d[4, 8, 100]}
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.