3
$\begingroup$

I would like to generate a table using a function of two variables. I tried the following:

Table[{a, b, f[a, b]}, {a, 0, 5}, {b, 0, 5}] 

which does work as I had hoped. I want the table to produce a, b, f[a, b] in columns using Grid. Thanks for suggestions!

$\endgroup$
2
  • $\begingroup$ Table[{a, b, f[a, b]}, {a, 0, 5}, {b, 0, 5}] // Grid ? Table[{a, b, f[a, b]}, {a, 0, 5}, {b, 0, 5}] // TableForm ? $\endgroup$
    – murray
    Commented Dec 10, 2016 at 22:43
  • $\begingroup$ Thank you, these work, but table iterates a1 b1, F[a1,b1] then a1,b2,F[a1,b2] etc I would like a1,b1,F[a1,b1] then a2,b2,F[a2,b2]. Can I do this? $\endgroup$
    – CMoller
    Commented Dec 11, 2016 at 0:02

2 Answers 2

5
$\begingroup$

It is not clear how many columns that you want. Some of the possibilities

data = Table[{a, b, f[a, b]}, {a, 0, 5}, {b, 0, 5}];

data // Flatten[#, 1] & // Grid

enter image description here

Row[Riffle[Grid /@ Partition[data // Flatten[#, 1] &, 18], Spacer[25]]]

enter image description here

Row[Riffle[Grid /@ Partition[data // Flatten[#, 1] &, 12], 
  Spacer[25]]]

enter image description here

$\endgroup$
2
  • $\begingroup$ Sorry I'm not used to this site and did not realize that Bob Hanion has given me a good answer. Thank you! $\endgroup$
    – CMoller
    Commented Dec 11, 2016 at 0:09
  • 2
    $\begingroup$ @user3429864: I'm confused - you accepted an answer which gives a result different from what you said you wanted in your comment responding to mine on your original post. By contrast, corey979's answer does give what you said in that comment. $\endgroup$
    – murray
    Commented Dec 12, 2016 at 1:44
2
$\begingroup$

Judging from your comment, you want this:

MapThread[{#1, #2, f[#1, #2]} &, {{1, 3, 5, 7, 9}, {2, 4, 6, 8, 10}}] // Grid

enter image description here


Or if the a's and b's take the same corresponding values:

Table[{a, a, f[a, a]}, {a, 1, 5}] // Grid

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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