Referencing an Item of an Array

If required, you can access a particular item of the array in an expression. You do this by using brackets [ ].

For example, 

Data1[Monday]

references a single (scalar) value representing the second item of the vector Data1 (defined by the set “Days”, which begins with Sunday).

Similarly,

Data2[Monday, Peaches]

references a single (scalar) value representing the item in the second row and third column of the matrix Data2 (with the rows defined by the set “Days”, and the columns defined by the set “Fruit”, which consists of Apples, Oranges and Peaches).

When referencing array items, you can also use variables as the arguments. For example, if Data1 was a vector of “Days”, and X was a scalar value, you could write the following in an expression:

Data1[X]

If X was 3, it would return the 3rd item in the vector (i.e., Data1[Tuesday]).

GoldSim rounds to the nearest integer when evaluating an array argument, so in this example, as long as the rounded value of X was between 1 and 7 inclusive, GoldSim would be able to evaluate the expression.  If X evaluated to a number outside of that range, GoldSim would display a fatal error. Of course, X could change with time (or any other variable in your model). 

This functionality can also be used with matrices.  As an example, if Data2 was a matrix with the rows defined by the set “Days”, and the columns defined by the set “Fruit” (with 3 entries), you could reference the following in an expression:

Data2[X, Y]

In this case, X would need to be an integer between 1 and 7, inclusive, and Y would need to be an integer between 1 and 3 inclusive.  If X was 3 and Y was 2, it would return the item in row 3, column 2 of the matrix (i.e., Data2[Tuesday, Peaches] assuming Peaches was the second array label for “Fruit”).

When using variables to reference array items, the following points should be noted:

   If the array is based on a named set, the variable represents the ordinal item in the set (i.e., the row or column number).  For example, if Data1 was vector of “Days”, and X was 3, then Data1[X] would return the 3rd item in the vector (i.e., Data1[Tuesday]).

   If the array is based on an indexed set, the variable represents the actual item with that number label in the set.  For examples, if Data9 was a vector defined by an indexed set with labels 3, 4, 5 and 6, and X was 3, then Data9[X] would return the first item in the vector (i.e., Data9[3]).

Finally, when using this with matrices, you can use * as a wildcard.  Using the example above,

 Data2[*, Y]

would return column Y (a vector of Days).  Similarly,

Data2[X, *]

would return row X (a vector of Fruit).

This ability to use variables when referencing array items is particularly powerful when using this feature in conjunction with array constructor functions.

Related Topics…

Learn more about: