2.9.8 Tables and Matrices

 TableForm[list] print in tabular form MatrixForm[list] print in matrix form

Formatting lists as tables and matrices.
Here is a list.
 In[1]:=  Table[(i + 45)^j, {i, 3}, {j, 3}]
 Out[1]=
TableForm displays the list in a tabular format.
 In[2]:=  TableForm[%]
 Out[2]//TableForm=
MatrixForm displays the list as a matrix.
 In[3]:=  MatrixForm[%]
 Out[3]//MatrixForm=
This displays an array of algebraic expressions as a matrix.
 In[4]:=  MatrixForm[ Table[x^i - y^j, {i, 3}, {j, 3}] ]
 Out[4]//MatrixForm=

 PaddedForm[TableForm[list], tot] print a table with all numbers padded to have room for tot digits PaddedForm[TableForm[list], {tot, frac}] put frac digits to the right of the decimal point in all approximate real numbers

Printing tables of numbers.
Here is a list of numbers.
 In[5]:=  fac = {10!, 15!, 20!}
 Out[5]=
TableForm displays the list in a column.
 In[6]:=  TableForm[fac]
 Out[6]//TableForm=
This aligns the numbers by padding each one to leave room for up to 20 digits.
In this particular case, you could also align the numbers using the TableAlignments option.
 In[8]:=  TableForm[fac, TableAlignments -> {Right}]
 Out[8]//TableForm=
This lines up the numbers, padding each one to have room for 8 digits, with 5 digits to the right of the decimal point.
 In[9]:=  PaddedForm[TableForm[{6.7, 6.888, 6.99999}], {8, 5}]

You can use TableForm and MatrixForm to format lists that are nested to any depth, corresponding to arrays with any number of dimensions.

Here is the format for a array of elements a[i, j].
 In[10]:=  TableForm[ Array[a, {2, 2}] ]
 Out[10]//TableForm=
Here is a array.
 In[11]:=  TableForm[ { Array[a, {2, 2}], Array[b, {2, 2}] } ]
 Out[11]//TableForm=
And here is a array.
 In[12]:=  TableForm[ { {Array[a, {2, 2}], Array[b, {2, 2}]}, {Array[c, {2, 2}], Array[d, {2, 2}]} } ]
 Out[12]//TableForm=

In general, when you print an -dimensional table, successive dimensions are alternately given as columns and rows. By setting the option TableDirections -> {, , ... }, where the are Column or Row, you can specify explicitly which way each dimension should be given. By default, the option is effectively set to {Column, Row, Column, Row, ... }.

The option TableDirections allows you to specify explicitly how each dimension in a multidimensional table should be given.
 In[13]:=  TableForm[ { Array[a, {2, 2}], Array[b, {2, 2}] }, TableDirections -> {Row, Row, Column} ]
 Out[13]//TableForm=

Whenever you make a table from a nested list such as {, , ... }, there is a question of whether it should be the or their elements which appear as the basic entries in the table. The default behavior is slightly different for MatrixForm and TableForm.

MatrixForm handles only arrays that are "rectangular". Thus, for example, to consider an array as two-dimensional, all the rows must have the same length. If they do not, MatrixForm treats the array as one-dimensional, with elements that are lists.

MatrixForm treats this as a one-dimensional array, since the rows are of differing lengths.
 In[14]:=  MatrixForm[{{a, a, a}, {b, b}}]
 Out[14]//MatrixForm=

While MatrixForm can handle only "rectangular arrays", TableForm can handle arbitrary "ragged" arrays. It leaves blanks wherever there are no elements supplied.

TableForm can handle "ragged" arrays.
 In[15]:=  TableForm[{{a, a, a}, {b, b}}]
 Out[15]//TableForm=
You can include objects that behave as "subtables".
 In[16]:=  TableForm[{{a, {{p, q}, {r, s}}, a, a}, {{x, y}, b, b}}]
 Out[16]//TableForm=

You can control the number of levels in a nested list to which both TableForm and MatrixForm go by setting the option TableDepth.

This tells TableForm only to go down to depth 2. As a result {x, y} is treated as a single table entry.
 In[17]:=  TableForm[{{a, {x, y}}, {c, d}}, TableDepth -> 2]
 Out[17]//TableForm=

 option name default value TableDepth Infinity maximum number of levels to include in the table TableDirections {Column, Row, Column, ... } whether to arrange dimensions as rows or columns TableAlignments {Left, Bottom, Left, ... } how to align the entries in each dimension TableSpacing {1, 3, 0, 1, 0, ... } how many spaces to put between entries in each dimension TableHeadings {None, None, ... } how to label the entries in each dimension

Options for TableForm.

With the option TableAlignments, you can specify how each entry in the table should be aligned with its row or column. For columns, you can specify Left, Center or Right. For rows, you can specify Bottom, Center or Top. If you set TableAlignments -> Center, all entries will be centered both horizontally and vertically. TableAlignments -> Automatic uses the default choice of alignments.

Entries in columns are by default aligned on the left.
 In[18]:=  TableForm[{a, bbbb, cccccccc}]
 Out[18]//TableForm=
This centers all entries.
 In[19]:=  TableForm[{a, bbbb, cccccccc}, TableAlignments -> Center]
 Out[19]//TableForm=

You can use the option TableSpacing to specify how much horizontal space there should be between successive columns, or how much vertical space there should be between successive rows. A setting of 0 specifies that successive objects should abut.

This leaves 6 spaces between the entries in each row, and no space between successive rows.
 In[20]:=  TableForm[{{a, b}, {ccc, d}}, TableSpacing -> {0, 6}]
 Out[20]//TableForm=

 None no labels in any dimension Automatic successive integer labels in each dimension {{, , ... }, ... } explicit labels