﻿ MODELS > Examples > Models Arrays

# Models Arrays

This example illustrates how to assign values to array variables.

MODEL arval   -- illustrates the use of array algebra in MODELS

VAR a[1..6] -- an array variable, with elements indexed from 1 to 6

EXEC

IF t=timestep THEN       -- letting contents execute only at the first time step

write("*** Begin results of model 'arval' ***")

a[1..3]          := 0                                         -- group assign

write("[0, 0, 0] : ", a[1],' ', a[2],' ', a[3])

a[1..3]          := [1, 2, 3]                             -- one-to-one assign

write("[1, 2, 3] : ", a[1],' ', a[2],' ', a[3])

a[4..6]          := [1, a[2..3] - [1, 2] ]             -- one-to-one subtract

write("[1, 1, 1] : ", a[4],' ', a[5],' ', a[6])

a[4..6]          := a[4..6] -1                               -- group subtract

write("[0, 0, 0] : ", a[4],' ', a[5],' ', a[6])

a[1..5]                := [1, 2, a[1..3]]     -- array ref inside array value

write("[1, 2, 1, 2, 3] : ", a[1],' ',a[2],' ',a[3],' ',a[4],' ',a[5])

a[1..5]                := [a[1..3], 1, 3]     -- array ref inside array value

write("[1, 2, 1, 1, 3] : ", a[1],' ',a[2],' ',a[3],' ',a[4],' ',a[5])

a[1..3]          := [1, 2, 3]                           -- assign array value

write("[1, 2, 3] : ", a[1],' ', a[2],' ', a[3])

a[4..6]          := a[1..3]                               -- assign array ref

write("[1, 2, 3] : ", a[4],' ', a[5],' ', a[6])

a[4..6]             := a[1..3] +10                               -- group add

write("[11, 12, 13] : ", a[4],' ', a[5],' ', a[6])

a[4..6]             := 10 +a[1..3]                               -- group add

write("[11, 12, 13] : ", a[4],' ', a[5],' ', a[6])

a[4..6]             := a[1..3] + [10, 10, 10]               -- one-to-one add

write("[11, 12, 13] : ", a[4],' ', a[5],' ', a[6])

a[4..6]                := a[1..3] -a[4..6]             -- one-to-one subtract

write("[-10, -10, -10] : ", a[4],' ', a[5],' ', a[6])

a[4..6]             := a[1..3] * 10                         -- group multiply

write("[10, 20, 30] : ", a[4],' ', a[5],' ', a[6])

a[4..6]             := 10 * a[1..3]                         -- group multiply

write("[10, 20, 30] : ", a[4],' ', a[5],' ', a[6])

a[4..6]             := a[1..3] / 0.1                           -- group divide

write("[10, 20, 30] : ", a[4],' ', a[5],' ', a[6])

a[4..6]                   := 10 / a[1..3]                     -- group divide

write("[10, 5, 3.3333333] : ", a[4],' ',a[5],' ',a[6])

a[4..6]          := a[1..3] AND [0, 0, 1] OR [0, 1, 0]   -- one-to-one and/or

write("[0, 1, 1] : ", a[4],' ', a[5],' ', a[6])

a[1..3]           := a[1..3]**2                             -- group exponent

write("[1, 4, 9] : ", a[1],' ', a[2],' ', a[3])

a[4..6]           := 10**a[4..6]                             -- group exponent

write("[1, 10, 10] : ", a[4],' ', a[5],' ', a[6])

a[4..6]           := a[4..6] {max:[100, 100, 8]}           -- one-to-one limit

write("[1, 10, 8] : ", a[4],' ', a[5],' ', a[6])

a[4..6]           := a[4..6] {max:8}                           -- group limit

write("[1, 8, 8] : ", a[4],' ', a[5],' ', a[6])

a[1..3]           := a[1..3] MOD 6                             -- group modulo

write("[1, 4, 3] : ", a[1],' ', a[2],' ', a[3])

a[1..3]           := 13 MOD a[1..3]                           -- group modulo

write("[0, 1, 1] : ", a[1],' ', a[2],' ', a[3])

write("*** End results of model 'arval' ***")

ENDIF

ENDEXEC

ENDMODEL