MATHEMATICA TUTORIAL

Making Tables of Values

You can use lists as tables of values. You can generate the tables, for example, by evaluating an expression for a sequence of different parameter values.

This gives a table of the values of , with running from 1 to 6.
In[1]:=
Click for copyable input
Out[1]=
Here is a table of for from to .
In[2]:=
Click for copyable input
Out[2]=
This gives the numerical values.
In[3]:=
Click for copyable input
Out[3]=
You can also make tables of formulas.
In[4]:=
Click for copyable input
Out[4]=
Table uses exactly the same iterator notation as the functions Sum and Product, which are discussed in "Sums and Products".
In[5]:=
Click for copyable input
Out[5]=
This makes a table with values of running from to in steps of .
In[6]:=
Click for copyable input
Out[6]=
You can perform other operations on the lists you get from Table.
In[7]:=
Click for copyable input
Out[7]=
TableForm displays lists in a "tabular" format. Notice that both words in the name TableForm begin with capital letters.
In[8]:=
Click for copyable input
Out[8]//TableForm=

All the examples so far have been of tables obtained by varying a single parameter. You can also make tables that involve several parameters. These multidimensional tables are specified using the standard Mathematica iterator notation, discussed in "Sums and Products".

This makes a table of with running from 1 to 3 and running from 1 to 2.
In[9]:=
Click for copyable input
Out[9]=

The table in this example is a list of lists. The elements of the outer list correspond to successive values of . The elements of each inner list correspond to successive values of , with fixed.

Sometimes you may want to generate a table by evaluating a particular expression many times, without incrementing any variables.

This creates a list containing four copies of the symbol .
In[10]:=
Click for copyable input
Out[10]=
This gives a list of four pairs of numbers sampled from . Table re-evaluates RandomSample[{1, 2, 3, 4}, 2] for each element in the list, so that you get four different samples.
In[11]:=
Click for copyable input
Out[11]=
This evaluates for each of the values of i in the list .
In[12]:=
Click for copyable input
Out[12]=
This creates a 3×2 table.
In[13]:=
Click for copyable input
Out[13]=
In this table, the length of the rows depends on the more slowly varying iterator variable, i.
In[14]:=
Click for copyable input
Out[14]=

You can use Table to generate arrays with any number of dimensions.

This generates a three-dimensional 2×2×2 array. It is a list of lists of lists.
In[15]:=
Click for copyable input
Out[15]=
Table[f,{imax}]give a list of values of f
Table[f,{i,imax}]give a list of the values of f as i runs from to
Table[f,{i,imin,imax}]give a list of values with i running from to
Table[f,{i,imin,imax,di}]use steps of di
Table[f,{i,imin,imax},{j,jmin,jmax},...]generate a multidimensional table
Table[f,{i,{i1,i2,...}]give a list of the values of f as i successively takes the values , , ...
TableForm[list]display a list in tabular form

Functions for generating tables.

You can use the operations discussed in "Manipulating Elements of Lists" to extract elements of the table.

This creates a table and gives it the name .
In[16]:=
Click for copyable input
Out[16]=
This gives the third part of the table.
In[17]:=
Click for copyable input
Out[17]=
This gives a list of the third through fifth parts.
In[18]:=
Click for copyable input
Out[18]=
This creates a 2×2 table, and gives it the name .
In[19]:=
Click for copyable input
Out[19]=
This extracts the first sublist from the list of lists that makes up the table.
In[20]:=
Click for copyable input
Out[20]=
This extracts the second element of that sublist.
In[21]:=
Click for copyable input
Out[21]=
This does the two operations together.
In[22]:=
Click for copyable input
Out[22]=
This displays in a "tabular" form.
In[23]:=
Click for copyable input
Out[23]//TableForm=
t[[i]] or Part[t,i]give the i^(th) sublist in t (also input as )
t[[i;;j]] or Part[t,i;;j]give a list of the parts i through j
t[[{i1,i2,...}]] or Part[t,{i1,i2,...}]give a list of the ^(th), ^(th), ... parts of t
t[[i,j,...]] or Part[t,i,j,...]give the part of t corresponding to

Ways to extract parts of tables.

As mentioned in "Manipulating Elements of Lists", you can think of lists in Mathematica as being analogous to "arrays". Lists of lists are then like two-dimensional arrays. When you lay them out in a tabular form, the two indices of each element are like its and coordinates.

New to Mathematica? Find your learning path »
Have a question? Ask support »