Table


When we use Table[expr, {i,imin,imax,di}] (imax-imin) doesn't have to be a
multiple of di.  See the following example.

Clear[a, x] ;  Table[a + dx, {dx, 0, 3, π/8}]

{a, a + π/8, a + π/4, a + (3 π)/8, a + π/2, a + (5 π)/8, a + (3 π)/4, a + (7 π)/8}


You can also give Table (imax, imin,di) that are not numeric as in the next
line.

Table[i, {i, -x, x, x/5}]

{-x, -(4 x)/5, -(3 x)/5, -(2 x)/5, -x/5, 0, x/5, (2 x)/5, (3 x)/5, (4 x)/5, x}

However, if you want to make a long list Range, can usually make in about half the time it takes Table to do the same.   In the lines below Range is used to make the lists in two examples above.

a + Range[0, 3, π/8]

{a, a + π/8, a + π/4, a + (3 π)/8, a + π/2, a + (5 π)/8, a + (3 π)/4, a + (7 π)/8}

x Range[-1, 1, 1/5]

{-x, -(4 x)/5, -(3 x)/5, -(2 x)/5, -x/5, 0, x/5, (2 x)/5, (3 x)/5, (4 x)/5, x}


The iterator in Table can be omitted if it isn't used.  The next line makes a
4x4 matrix of real numbers.  This can save considerable time.  For example
Table[Random[],{1000},{100}] evaluates in less than half the time it takes to
evaluate Table[Random[],{i,1000},{j,100}].

Table[Random[], {4}, {4}]

{{0.846124, 0.205531, 0.725948, 0.311959}, {0.65191, 0.179446, 0.0417905, 0.129435}, {0.11863, 0.535591, 0.359989, 0.447071}, {0.65219, 0.552745, 0.83467, 0.185322}}


The following construct can be used to make a list of lists that grow in
length.

Table[1/(x + y), {x, 6}, {y, x}]//TableForm

1/2
1/3 1/4
1/4 1/5 1/6
1/5 1/6 1/7 1/8
1/6 1/7 1/8 1/9 1/10
1/7 1/8 1/9 1/10 1/11 1/12

Created by Mathematica  (May 16, 2004)

Back to Ted’s Tricks index page