Array Indexing

1 General Description

The indexing operator ....[....] takes two or more operands, where the first operand is the array to be indexed and the rest of the operands are index expressions. It is used to access array elements for retrieval of their values or for updating these values. In class ArrayIndex: arrayname[indexexpr1, indexexpr2, ......];


1.1 ArrayIndex Class


1.2 Simulation of ArrayIndex

Here we simulate the class ArrayIndex above.



We see that B[2, 1] now has the value 8.




2 Indexing with Scalar Index Expressions

Array indexing using scalar integer index expressions is usually used to retrieve or update single array elements. The value of the index expression for the i:th dimension of an array X must be within the bounds of the size of that dimension, i.e. 1.Since multi-dimensional arrays are defined as arrays of arrays in Modelica, it is also possible to access sub-arrays using simple scalar indexing. For example, the array b can also be regarded as a one-dimensional array of vectors.





3 Accessing Array Slices with Index Vectors

When at least one of the index expressions in an array indexing operation is a vector expression, a subsection (slice) of the array is accessed rather than a single element. Such an index vector must be a vector of integer values, where each value must be within the size bounds for the particular array dimension. An index vector can be constructed in any way but it is often convenient to use the colon operator to construct a range vector of index values. A single colon without operands used as an index expression at a dimension k of an array of X denotes all indices of that dimension and is equivalent to the index vector 1:size(X, k).




4 Array Field Slices of Record Arrays

If A is an array of records and m is a field of that record type, the expression A.m is interpreted as an array field slice operation of that record array. It returns the array of fields: {A[1].m, A[2].m, A[3].m, ...}. For example, given a record Person with three fields: name, age and children.


4.1 Record Person, Function mkperson and Class p



We have defined a record constructor function:
mkperson(name, age, children) in Modelica returning a Person record, which is used to define the contents of a small array persons consisting of Person records.



4.2 Calling mkperson


4.3 Classes PersonList and getPerson



The field slices
persons.name, persons.age and persons.children give the following results: