**2.8.8 Tables and Matrices**

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=

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[7]:= **PaddedForm[TableForm[fac], 20]**

Out[7]//PaddedForm=

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}]**

Out[9]//PaddedForm=

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=

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=

Settings for the option TableHeadings.

This puts integer labels in a

array.
In[21]:= **TableForm[Array[a, {2, 2, 2}],**

TableHeadings -> Automatic]

Out[21]//TableForm=

This gives a table in which the rows are labeled by integers, and the columns by a list of strings.
In[22]:= **TableForm[{{a, b, c}, {ap, bp, cp}},**

TableHeadings ->

{Automatic, {"first", "middle", "last"}}]

Out[22]//TableForm=

This labels the rows but not the columns. TableForm automatically inserts a blank row to go with the third label.
In[23]:= **TableForm[{{2, 3, 4}, {5, 6, 1}},**

TableHeadings ->

{{"row a", "row b", "row c"}, None}]

Out[23]//TableForm=