Arrays - Part 2 of 2
Re-formatting Arrays
Normally, Analytica automatically controls the row and column choices that determine the order of dimensions, or format, in which a table will be displayed to the user. This information is stored in User Interface attributes Reformdef and Reformval. However, there may be occasions when you wish to control the format of array, for example via IAC. There are two main methods for reformatting arrays: using the Reform function, and controlling tabletype and delimiter system variables.
The Reform Function
The Reform function changes the displayed structure of an array. Reform is useful in altering tables to look the way you want them to look.
Reform(x, [i1, i2. . .in]) prints out an n-dimensional array with index i n along rows, i [n-1] down columns, and the rest of the indices (if any) choosing the sequence of tables (2-D slices) from the array, so that the earliest index varies most slowly. This is to allow you to print out 2 or more dimensional arrays in whatever sequence you like most. The first argument must be an array with 2 or more dimensions. The second argument is a list of Indices, which must be indices of x, but may be in any order.
Example>mpy:[8k 12k 15k] Example>mpg:[26 30 35] Example>gasprice:1 Example>fuelcost:gasprice*mpy/mpg Example>mid fuelcost Mpg 26, 30, 35 Mpy 8000 [ 307.692, 266.667, 228.571] 12K [ 461.538, 400, 342.857] 15 [ 0.57692, 0.50000, 0.42857] Example>reform(fuelcost, [mpg, mpy]) Mpy 8000, 12K, 15 Mpg 26 [ 307.692, 461.538, 0.57692] 30 [ 266.667, 400, 0.50000] 35 [ 228.571, 342.857, 0.42857]
Controlling Tabletype and Delimiter
A variable value result that is an array of two or more dimensions is usually shown in a readable format designed for Typescript viewing. This format is not ideal when the result is intended for export to another spreadsheet program, for copying and pasting within Analytica, or for IAC. Two system variables in Analytica, Tabletype and Delimiter, make it easier to read values of variables back into Analytica, into other spreadsheet-oriented programs, or for IAC.
A system variable called Tabletype controls the formatting and content for the display of values in the typescript, regardless of whether they are deterministic or probabilistic, a simple array, or a higher-dimensional table.
Setting Tabletype to 0 displays tables in the format that is readily understandable when viewed in the typescript (default). Setting Tabletype to 1 displays tables in a format that can be used as a Analytica definition. Setting Tabletype to 2 prints out tables in a format that can be used to import data into a spreadsheet or other data analysis program, or for IAC.
For example, with the default Tabletype of 0, tables are printed in the usual manner.
Carcost>value Fuelcost Mpg 25, 30, 35 Mpy 12K [ 576, 480,411.429] 18K [ 864, 720,617.143] 24K [ 1152, 960,822.857]
When we set the Tabletype to 1, tables are printed in a format that Analytica can accept as a valid definition.
Carcost>tabletype: 1 Carcost>value Fuelcost Table(Mpy,Mpg)( { Notice that the values of indices are not displayed } 576, 480,411.429, 864, 720,617.143, 1152, 960,822.857 )
You can take such a table, copy it using the Copy menu command in the Edit menu (C), and paste it into the input portion of the Analytica typescript, i.e., click in the input portion and use the Paste menu command in the Edit menu (V). You can do the same to copy and paste a definition into an Analytica model text file.
With Tabletype set to 2, tables are printed in a format that you can use to copy and paste into a spreadsheet program or another data analysis program.
Carcost>tabletype: 2 Carcost>value Fuelcost Mpg 25 30 35 Mpy 12K 576 480 411.429 18K 864 720 617.143 12K 1152 960 822.857
This is a tab-delimited table of five rows and four columns, with the second row containing only one column. Follow the same Copy and Paste commands described above to copy a table from Analytica into a spreadsheet or other data analysis package. This tab-delimited format is also ideal for IAC.
If you wish to export the value of an Analytica variable into another program such as a spreadsheet program, a graphing package, or a statistical package, you need to have the data in a format that the other program can read in easily. Most of these programs recognize text files containing rows and columns of text and numbers separated by special characters called delimiters. Tabletype=2 prints tables in a similar format to the Tabletype=0 format, except it doesn’t presume commas for its delimiter and doesn’t print out brackets surrounding the one-dimensional parts of the table. Instead, Tabletype=2 uses another system variable called Delimiter which separates items in the table.
Delimiter=0 uses commas in tables which some programs require for recognizing input. Delimiter=1 uses tab characters which can’t be seen but are printed in the Analytica typescript and are the most common delimiter type (default and shown in the example in this section). Delimiter=2 uses space characters which are another common delimiter type in table-oriented programs. System variable Delimiter only applies to Tabletype=2 since both the typescript-formatted and Analytica-formatted tables rely on using comma delimiters.
- Note In order to create true x-y graphs (in some programs, such as graphing programs and spreadsheets) you must provide an additional x column for numerical data following the first column. For these programs, you can use Tabletype: 3. The output is identical to Tabletype: 2, except the first column is repeated twice when the column contains numbers.
Arrays: Summary
Array(i1, i2, ... in, y)
A function, which assigns a list of indices, i1, i2, ... in, as the indices of the array y, with i1 as the index of the outermost dimension, i2 as the second outermost, etc. y must have at least n dimensions.
Extraction
You can "pull out" specific values from an array by requesting a particular index and optionally, particular values in the index (e.g., cost[mpg=15]).
Slice(a, i, n)
Function that returns the nth value of array a over the dimension indexed by i. n must be between 1 and the length of i. n may also be an array of values, in which case, Analytica will return an array of corresponding values from a.
Subscript(a, i, x)
Function which gives the element of array a for which index i has value x. x must be one of the values of index i. x may also be an array of values from index i, in which case it will produce a corresponding array of resulting values from a. (It is essentially the same as a[i=x], but allows a to be a general expression, instead of restricting it to a variable.)
Size(x)
A function that returns the number of elements of the outermost dimensions of an array x.
Table(i1, i2, ... in) (x1, x2, x3, ... xm)
This function creates an n-dimensional array, indexed by the Indices i1, i2, ... in. The number of indices may be 1 or more. The indices must be separated by commas and enclosed in parentheses, as shown. The second set of parameters to Table specify the values that go into the array. These are also enclosed in parentheses, and the separating commas are optional. Each of these values is specified by an expression x. The number of values required is the number of elements of the array, which is the product of the sizes of all the dimensions. In this list of elements, the last index in is the innermost, varying most rapidly.
Reform(x, [i1, i2. . .in])
Command to print a multi-dimensional array x in a sequence so that index i1 is varying slowest, i2 next slowest and so on. The indices i1, i2, etc., must be all of the indices of x.
See Also
Enable comment auto-refresher