1
$\begingroup$

In the code below, how can I add (left to right) column headings "n", "pp","aa"? There is also a shift in the alignment across the generated rows.

Clear[a, pp, aa, n]
a = 15;
P[n_] := (1/2)^n
Payoff[n_] := 10 Exp[-n]
pp = N[Table[{n, P[n]*Payoff[n]}, {n, 1, a}]];
aa = N[Accumulate[Table[P[n]*Payoff[n], {n, 1, a}]]];
TableForm[Table[{pp, aa}, 1]]
$\endgroup$
3
  • $\begingroup$ Have you seen the TableHeadings option of TableForm[]? $\endgroup$ Commented Nov 16, 2019 at 17:42
  • $\begingroup$ yes; but I think I need to use some form of Prepend or MapThread. It should be easy but adding these headers can get tricky ... prg $\endgroup$
    – user42700
    Commented Nov 16, 2019 at 17:56
  • 1
    $\begingroup$ "I think I need to use some form of Prepend or MapThread" - ponder the result of TableForm[Partition[Range[9], 3], TableHeadings -> {{"a", "b", "c"}, {"d", "e", "f"}}], and consult the docs for TableForm[] if you want further information. $\endgroup$ Commented Nov 16, 2019 at 17:59

1 Answer 1

2
$\begingroup$
Clear[a, pp, aa, n, P, Payoff]
a = 15;
P[n_] := (1/2)^n
Payoff[n_] := 10 Exp[-n]
pp = Table[{n, N[P[n]*Payoff[n]]}, {n, 1, a}];
aa = N[Accumulate[Table[P[n]*Payoff[n], {n, 1, a}]]];

TableForm[Flatten /@ Transpose[{pp, aa}],
 TableHeadings -> {None, {"n", "pp", "aa"}}]

enter image description here

$\endgroup$
1
  • $\begingroup$ Fantastic! ... many thanks Bob, ... prg $\endgroup$
    – user42700
    Commented Nov 16, 2019 at 18:13

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