Arrays - Part 1 of 2

This chapter describes how to access arrays and control the format of arrays for output. Before reading this chapter, you should already be familiar with arrays as described in the Analytica Reference.

Arrays are created from a variety of sources. Two functions for creating arrays include Table and Array. Also, variables and Index variables defined as Lists (e.g., for parametric analysis) are arrays.

The Table Function

In parametric analysis, two and three-dimensional Arrays can be generated from expressions containing one, two, or three Index variables. The Table Function is designed to permit you to input a Table directly and specify its Indices within one variable. The parameters to Table must be enclosed in parentheses, as shown below:

  Example>Mpg:Table(Cartype) (32, 34, 18)
  Example>value mpg
     Cartype        vw,   honda,     bmw
             [      32,      34,      18]

You may specify more than one Index variable in a Table. The number of Index variables specifies the number of Dimensions. The number of values in the Table (specified in the second list of parameters) must equal the product of the numbers of elements for every dimension. For instance, if the first parameter of the Table is two Indices of three elements each, the second parameter must have nine elements.

  Example>variable yr
  Description:model year of car
  Definition:[1985, 1986, 1987, 1988]
  Example>variable car_prices
  Title:prices for cars
  Description:Prices for three brands of cars over a three-year period.
  Definition:table(cartype, yr)(8K, 9K, 9.5K, 10K 12K, 13K, 14K, 14.5K 18K, 20K, 21K, 22K)
  Example>value car-prices
   Yr               1985,    1986,    1987,    1988
            vw [    8000,    9000,    9500,     10K]
         honda [     12K,     13K,     14K, 14.500K]
           bmw [     18K,     20K,     21K,     22K]

The Array Function

The Array Function is similar to the Table Function, in that it can be used to specify an Array directly, but its syntax is a bit different. Like Table, the first n parameters are the Index variables that specify the n dimensions of the result. The main difference is that the values to be the elements of the Array are listed in square brackets as the last parameter (instead of as an extra list between parentheses as in Table). For example:

  Example>Mpg: Array(Cartype, [32, 34, 18])
   Cartype      VW,  Honda,    BMW
          [     32,     34,     18]

If the Array has multiple dimensions, then the elements are listed in nested brackets, following the structure of the Array as a list of lists (of lists ... etc., according to the number of dimensions):

  Example>variable Car_prices
  Title: Prices for car
  Units: $
  Description: Prices for three brands of car over four year period.
  Definition: Array(Cartype, Yr, [[8K, 9K,  9.5K,10K  ], [12K, 13K, 14K, 14.5K],[18K, 20K, 21K, 22K  ]])

As in Table, the Index variables are specified from the outer to innermost. In the above example Cartype comes before Yr, and so the Array is specified as a list (indexed by CarType) of lists (each indexed by Yr). The result looks the same as before:

  Example>value car_prices

   Yr              1985,   1986,   1987,   1988
            VW [   8000,   9000,   9500,    10K]
         Honda [    12K,    13K,    14K,14.500K]
           BMW [    18K,    20K,    21K,    22K]

Note that the size of each list in square brackets must match the size of the corresponding Index. In this case there is a list of three elements (for the three car types), and each element is a list of four elements (for the four years). Analytica will complain if these sizes don't match. Size is discussed below.

Size of a Dimension

The function Size returns the number of elements of a list:


If it is applied to a multi-dimensional array, it returns the number of elements in all dimensions:

Example>size([[1,2,3], [4,5,6]])

Selecting Parts of an Array

If you want to extract a single element of an array, or a column or a row of a table, you can specify the index and its value in square brackets after the array. Suppose we reassign a single value to Price in the Fuel Cost model, so that Cost is again 2 dimensional:


  Example>value FuelCost
     Mpg              24,     28,     32,     36
           200 [8.7500, 7.5000, 6.5625, 5.8333]
           250 [10.937, 9.3750, 8.2031, 7.2916]
           300 [13.125, 11.250, 9.8438, 8.7500]

You can extract a single row or column from this table by specifying the index and its value in square brackets, after the variable name:

   Mpg        24,     28,     32,     36
         [10.937, 9.3750, 8.2031, 7.2916]

   Distan    200,    250,    300
         [5.8333, 7.2916, 8.7500]

You can also select over more than one index dimension at a time. The index value specifications are separated by comma(s).

  Example>FuelCost[Distance=250, Mpg=36]

Note that in most computer languages you need to know which index is attached to which dimension of the array, and you have to specify them in the right order, row index before column index or vice versa. In Analytica you don't need to remember this, and instead specify the indexes by identifier in whatever order you like.

The Slice Function

The Slice Function returns a cross-section of an array. The syntax is: Slice(a, i, n) - Analytica returns the part of array a that corresponds to index variable i and is restricted to the nth elements of i, as shown below:

  Example>sho cartype
  variable Cartype
  Title: car brands
  Units: names
  Definition: ['vw', 'honda', 'bmw']
  Example>sho price
  variable Price
  Title:  Car prices for each of three types of cars.
  Units:  $
  Description:  Total purchase price for three cars.
  Definition:  array(cartype, [5K, 10K, 15K])
  Example>'''mid cost'''   ''(Cost is an Output of Price.)
   Cartype              vw,     honda,       bmw
            15 [      2185,      2810,      3435]
            30 [      1705,      2330,      2955]
            40 [      1585,      2210,      2835]
  Example>'''slice(cost, cartype, 1)'''
   Mpg                15,        30,        40
             [      2185,      1705,      1585]

In the example above, Analytica returns the values corresponding to the first element of Cartype (i.e., the values of VW). Note: the third parameter of Slice can be an array, too.

  Example>slice(cost, cartype, [1, 2])
   Mpg                  15,        30,        40
             1 [      2185,      1705,      1585]
             2 [      2810,      2330,      2210]

The Subscript Function

Subscript(a, i, x) - This is very similar to Slice; however, instead of referring to the position of the element(s) in i, Subscript refers to the actual value, x within i.

  Example>mid cost      (Cost is an Output of Price.)
   Cartype              vw,     honda,       bmw
            15 [      2185,      2810,      3435]
            30 [      1705,      2330,      2955]
            40 [      1585,      2210,      2835]

  Example>subscript(cost, mpg, 15)
   Cartype            vw,     honda,       bmw
             [      2185,      2810,      3435]

Example>subscript(cost, cartype, ['vw', 'honda'])
   Mpg                  15,        30,        40
             1 [      2185,      1705,      1585]
             2 [      2810,      2330,      2210]

Subscript permits you to use arbitrary expressions as the first parameter. For instance:

  Example>mpg:[32 34 18]
  Example>'subscript(cost/12, mpg, 32)
   Price             10K,     15K,     25K
            vw [ 193.229, 245.313, 349.479]
         honda [ 176.302, 217.969, 301.302]
           bmw [ 156.399, 186.161, 245.685]

If you tried to type this expression without using the Subscript Function, Analytica would have printed an error message:

  ? Expecting "end of line".   
        ^?  Syntax error:

See Also


You are not allowed to post comments.