Array

In the first line below we get a list of Cos[x/4] sampled at
x={1, 2, 3, ... 12}.  You can get the same result using Cos[Range[1/4,3,1/4]], and timing tests show it's faster to do this using Range.  If you are concerned about speed, you should use Range whenever possible.  The #& notation is explained under Function.

Array[Cos[#/4] &,   12]

{Cos[1/4], Cos[1/2], Cos[3/4], Cos[1], Cos[5/4], Cos[3/2], Cos[7/4], Cos[2], Cos[9/4], Cos[5/2], Cos[11/4], Cos[3]}

The next line gives 12 samples of Cos[x/4] starting at (x=4).

Array[Cos[#/4] &,   12, 4]

{Cos[1], Cos[5/4], Cos[3/2], Cos[7/4], Cos[2], Cos[9/4], Cos[5/2], Cos[11/4], Cos[3], Cos[13/4], Cos[7/2], Cos[15/4]}

Array can take a fourth argument which should be applied to the result instead of list.  The next line finds the minimum of the samples given in the previous line.  Notice the function to be applied must be the fourth argument, so you must provide an starting value as a third argument.

Array[Cos[#/4] &, 12, 4, Min]

Cos[13/4]

The starting place for Array doesn't have to be an integer.  In the next line Array starts at the symbol (t) and samples Cos[_] at 11 points.

ClearAll[f, t] ; Array[Cos, 11, t]

{Cos[t], Cos[1 + t], Cos[2 + t], Cos[3 + t], Cos[4 + t], Cos[5 + t], Cos[6 + t], Cos[7 + t], Cos[8 + t], Cos[9 + t], Cos[10 + t]}

In the next line Array is used to make a matrix.  Range can do many things Array can do very efficiently.  However, Array is the method of choice for making matrices such as the following.

Array[f, {5, 3}]//MatrixForm

( f[1, 1]   f[1, 2]   f[1, 3] )            f[2, 1]   f[2, 2]   f[2, 3]         ...    f[3, 2]   f[3, 3]            f[4, 1]   f[4, 2]   f[4, 3]            f[5, 1]   f[5, 2]   f[5, 3]

In the line above  both arguments of (f) start at one (the default).  
Using the next line both arguments of (f) start at 10.

Array[f, {5, 3}, 10]//MatrixForm

( f[10, 10]   f[10, 11]   f[10, 12] )            f[11, 10]   f[11, 11]   f[11, ... [12, 12]            f[13, 10]   f[13, 11]   f[13, 12]            f[14, 10]   f[14, 11]   f[14, 12]

In the next line the array starts with 4 in the first argument and 10 in the second argument and counts from there

Array[f, {5, 3}, {4, 10}]//MatrixForm

( f[4, 10]   f[4, 11]   f[4, 12] )            f[5, 10]   f[5, 11]   f[5, 12]   ... 11]   f[6, 12]            f[7, 10]   f[7, 11]   f[7, 12]            f[8, 10]   f[8, 11]   f[8, 12]


Created by Mathematica  (May 16, 2004)

Back to Ted’s Tricks index page