1
$\begingroup$

I have a Table consisting of an independent variable and two functions. I want to output it like this:

x   1    2    3    4    5  ...
f f(1) f(2) f(3) f(4) f(5) ...
g g(1) g(2) g(3) g(4) g(5) ...

but doing

Grid[Table[...]]

arranges the values vertically. Any way around this?

$\endgroup$
  • 3
    $\begingroup$ Look up Transpose $\endgroup$ – Sektor Feb 12 '15 at 21:35
  • $\begingroup$ See also TableDirections option of TableForm. $\endgroup$ – Mr.Wizard Feb 13 '15 at 10:28
  • $\begingroup$ Possible duplicates: (46483), (48821). Also see: (5011242) $\endgroup$ – Mr.Wizard Feb 13 '15 at 10:34
2
$\begingroup$

This is a canonical way of getting what you want:

data = Table[i[j], {i, {f, g}}, {j, 1, 5}];
TraditionalForm@TableForm[
  data,
  TableHeadings -> {{f, g}, Range[5]}]

\begin{array}{c|ccccc} \text{} & 1 & 2 & 3 & 4 & 5 \\ \hline f & f(1) & f(2) & f(3) & f(4) & f(5) \\ g & g(1) & g(2) & g(3) & g(4) & g(5) \\ \end{array}

Sorry I can't easily get the "x" in the top. If that's important, the other answer is better.

| improve this answer | |
$\endgroup$
0
$\begingroup$

Although there are probably slightly more elegant answers, the following works:

gridDraw[N_] := 
  Module[{aRange = Range[N], bRange = Range[N], cRange}, PrependTo[bRange, ""]; 
Grid[{bRange, Flatten[{f, f /@ aRange}], Flatten[{g, g /@ aRange}]},
Frame -> All]]

By just calling:

gridDraw[5]

You get exactly the same as above (apart from the square parentheses):

\begin{array}{cccccc} \text{} & 1 & 2 & 3 & 4 & 5 \\ f & f(1) & f(2) & f(3) & f(4) & f(5) \\ g & g(1) & g(2) & g(3) & g(4) & g(5) \\ \end{array}

Hope that helps.

| improve this answer | |
$\endgroup$

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