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 (i^2), with i running from 1 to 6:

Table[i^2,{i,6}]

Here is a table of sin(n/5) for n from 0 to 4:

Table[Sin[n/5],{n,0,4}]

This gives the numerical values:

N[%]

You can also make tables of formulas:

Table[x^i+2i,{i,5}]

(RefLink[Table, paclet:ref/Table]) uses exactly the same iterator notation as the functions (RefLink[Sum, paclet:ref/Sum]) and (RefLink[Product, paclet:ref/Product]), which are discussed in "Sums and Products":

Product[x^i+2i,{i,5}]

This makes a table with values of x running from 0 to 1 in steps of 0.25:

Table[Sqrt[x],{x,0,1,0.25}]

You can perform other operations on the lists you get from (RefLink[Table, paclet:ref/Table]):

%^2+3

(RefLink[TableForm, paclet:ref/TableForm]) displays lists in a "tabular" format. Notice that both words in the name (RefLink[TableForm, paclet:ref/TableForm]) begin with capital letters:

%//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 Wolfram Language iterator notation, discussed in "Sums and Products".

This makes a table of (x^( i))+(y^( j)) with i running from 1 to 3 and j running from 1 to 2:

Table[x^i+y^j,{i,3},{j,2}]

The table in this example is a list of lists. The elements of the outer list correspond to successive values of i. The elements of each inner list correspond to successive values of j, with i 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 x:

Table[x,{4}]

This gives a list of four pairs of numbers sampled from {1,2,3,4}. (RefLink[Table, paclet:ref/Table]) reevaluates (RefLink[RandomSample, paclet:ref/RandomSample])[{1,2,3,4},2] for each element in the list, so that you get four different samples:

Table[RandomSample[{1,2,3,4},2],{4}]

This evaluates (Sqrt[i]) for each of the values of i in the list {1,4,9,16}:

Table[(Sqrt[i]),{i,{1,4,9,16}}]

This creates a 3×2 table:

Table[i+2j,{i,3},{j,2}]

In this table, the length of the rows depends on the more slowly varying iterator variable, i:

Table[i+2j,{i,3},{j,i}]

You can use (RefLink[Table, paclet:ref/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:

Table[i j^2 k^3,{i,2},{j,2},{k,2}]

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 sq:

sq=Table[j^2,{j,7}]

This gives the third part of the table:

sq[[3]]

This gives a list of the third through fifth parts:

sq[[3;;5]]

This creates a 2×2 table, and gives it the name m:

m=Table[i-j,{i,2},{j,2}]

This extracts the first sublist from the list of lists that makes up the table:

m[[1]]

This extracts the second element of that sublist:

%[[2]]

This does the two operations together:

m[[1,2]]

This displays m in a "tabular" form:

TableForm[m]

Ways to extract parts of tables.

As mentioned in "Manipulating Elements of Lists", you can think of lists in the Wolfram Language 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 x and y coordinates.