5
$\begingroup$

I have:

g = #1^2 + a #2^3 / #1 &

Now I wish to make a Table evaluating this from 1 to 10

Table[ g, { #, 1, 10}] 

fails.

What is the correct syntax?

$\endgroup$
4
$\begingroup$

Maybe Array is more suitable than Table for this task:

g = #1^2 + a #2^3/#1 &;
Array[g, {10, 10}]
$\endgroup$
3
$\begingroup$

You cannot use Slot (#) as an iterator. You have to apply an argument:

g = #1^2 + a #2^3/#1 &;
Table[g[i, i], {i, 1, 10}]
$\endgroup$
  • $\begingroup$ Thanks to all for the helpful answers $\endgroup$ – CMoller Sep 20 '14 at 0:08
1
$\begingroup$

Map is another concise alternative:

g = #1^2 + a #2^3/#1 &;
g[#, #] & /@ Range[10]
$\endgroup$
1
$\begingroup$

If you wish to iterate on the first argument from 1 to 10 to get a list of 10 functions, you can use

Table[With[{i = i}, g[i, #] &], {i, 1, 10}]
(* {g[1, #1] &, g[2, #1] &, g[3, #1] &, g[4, #1] &, g[5, #1] &, 
    g[6, #1] &, g[7, #1] &, g[8, #1] &, g[9, #1] &, g[10, #1] &} *)

If you want to iterate both arguments from 1 to 10 (on the diagonal), you can use Table as in halirutan's answer, or Array

Array[g[#, #] &, {10}]
(* {15, 60, 135, 240, 375, 540, 735, 960, 1215, 1500} *)

For iterating each of the two arguments separately you can use Array[g, {10, 10}] as in Karsten 7.'s answer, or

Table[g[i, j], {i, 10}, {j, 10}]
Table[g[i, j], {i, 10}, {j, 10}] ==Array[g, {10, 10}]
(* True *)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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