# How to Index in TableForm

Why are the k-values 0,10 not showing in the TableForm below?

TableForm[RecurrenceTable[{u[k + 1] == (9/10)*u[k] + s[k] + k,
s[k + 1] == (9/10)*s[k] + (1/10)*u[k], u == 50, s == 20}, {u,
s}, {k, 0, 10}], TableHeadings -> {{}, {"k","u[k]", "s[k]"}}]


## 2 Answers

You can't simply refer to k outside the scope of RecurrenceTable, but there are plenty of ways to add in a value.

TableForm[MapIndexed[Flatten[{#2 - 1, #1}] &,
RecurrenceTable[{u[k + 1] == (9/10)*u[k] + s[k] + k,
s[k + 1] == (9/10)*s[k] + (1/10)*u[k], u == 50, s == 20}, {u,
s}, {k, 0, 10}]], TableHeadings -> {None, {"k", "u[k]", "s[k]"}}]


or

{kmin, kmax} = {0, 10};
TableForm[MapThread[Prepend,
{RecurrenceTable[{u[k + 1] == (9/10)*u[k] + s[k] + k,
s[k + 1] == (9/10)*s[k] + (1/10)*u[k], u == 50, s == 20}, {u,
s}, {k, kmin, kmax}], Range[kmin, kmax]}], TableHeadings -> {None,
{"k", "u[k]", "s[k]"}}]

• All: Each of your recommendations was very innovative and most helpful; many thanks to all ... prg – PRG Nov 4 '19 at 0:13

I don't have any reputation so I can't comment on or upvote Chris's answer, consider this an addendum (with an additional solution at the end):

We know TableForm should have a column of data for each heading (in this case 3 columns).

Dimensions of original data (11 rows, 2 columns):

In[]:= Dimensions@
RecurrenceTable[{u[k + 1] == (9/10)*u[k] + s[k] + k,
s[k + 1] == (9/10)*s[k] + (1/10)*u[k], u == 50, s == 20}, {u,
s}, {k, 0, 10}]

Out[]= {11, 2}



Dimensions of Chris's Data (11 rows, 3 columns):

In[]:= Dimensions@
MapIndexed[Flatten[{#2 - 1, #1}] &,
RecurrenceTable[{u[k + 1] == (9/10)*u[k] + s[k] + k,
s[k + 1] == (9/10)*s[k] + (1/10)*u[k], u == 50,
s == 20}, {u, s}, {k, 0, 10}]]

Out[]= {11, 3}


Here's another way to get 3 columns:

TableForm[
count = 0;
Table[Prepend[row, count++], {row, #}] &@
RecurrenceTable[{u[k + 1] == (9/10)*u[k] + s[k] + k,
s[k + 1] == (9/10)*s[k] + (1/10)*u[k], u == 50,
s == 20}, {u, s}, {k, 0, 10}],
TableHeadings -> {{}, {"k", "u[k]", "s[k]"}}]