element function
element([X],I)
Picks the Ith value from the array [X]
Inputs: an array [X] of any type
integer I
Result: type
Examples:
element([10,20,30,40],3) --> 30 (since 30 is the value of the 3rd element of the array.
element([[1,2], [3,4], [5,6]],2) --> [3,4] (since the array [3,4] is the 2nd element of the input array)
element([10,20,30,40],index(1)) --> 10 for the first instance of a four-instance submodel, 20 for the second instance, etc, since index(1) has the value 1 for the first instance, 2 for the second instance, etc.
Comments:
This is an essential function for use with multiple-instance submodels, in which case it is almost always used in combination with the function index(1) in the second argument. One common use is to provide each instance of a multiple-instance submodel with a unique value for some parameter or other value. The third example (above) illustrates this: that could, for example, be the expression in a compartment element inside a four-instance submodel, initialising the compartment for each of the four instances to 10, 20, 30 and 40 respectively.
For advanced users:
The element function has rather more power than suggested above. The second argument can act as a sort of template to say how values (or sub-arrays) from the first argument are to be picked up. This is illustrated by the following two examples:
element([3,2,7,4,9,34,1,5], [[5,2], [1,5]]) --> [[9,2], [3,9]]
element([[5,7],[1,4],[8,5]], [3,3,2,2]) --> [[8,5], [8,5], [1,4], [1,4]]
In: Contents >> Working with equations >> Functions >> Built-in functions