Tables in Mathematica

Amy DeCelles

Using the Table and Grid command

The Table command can be used to generate arrays of numbers.  For example, if I want to create a list of numbers from 1 to 10, I would use the following command:

MyNumbers=Table[n, {n, 1, 10}]

If I wanted to create a two-column table with values n and (n^2), I would use the following command:

Squares = Table[{n, n^2}, {n, 1, 10}]

As you can see, the Mathematica output of the Table command gives a list of lists, using curly braces.  If I wanted to see the output in a two-column table, I could use the TableForm command:

TableForm[Squares]

If I wanted to see the table values in a grid, I would use the Grid command:

Grid[Squares, Frame->All]

You can also change the background color, the alignment, and other things with the Grid command.  Take a look at the help documentation if you’re interested.

Use the Prepend command to add a row of headers to the grid:

Grid[Prepend[Squares, {"Integer", "Square"}], Frame->All]

Using these commands you can create tables with more and more columns.  For example, if you were interested in looking at the remainder of an integer squared after division by 7 and comparing to the square of the remainder (as in one of our the homework exercises), you could make the following table:

SquaresRemainders = Table[{n, n^2, Mod[n,7] , Mod[n,7]^2,Mod[n^2, 7],  Mod[Mod[n,7]^2, 7]}, {n, 1, 10}]

Grid[Prepend[SquaresRemainders, {"n=7a+r", "(n^2)=7b+s", "r", "(r^2)=7c+t", "s","t" }], Frame->All,Spacings->{2,1}, ItemStyle->"Text"]

Now this table looks nice enough that you could copy and paste this and include it as a table in a report.  Of course, whenever you include a table (or a graph for that matter) you should introduce it with a verbal description.  In this case you would need to say what n, r, s, and t refer to.