Indices

Indices can be used on any numeric variable, except scalars, and on any string variable. A string variable is a string, and can have only one index, which is interpreted as a character index. A string array variable is an array of strings, and can have either one or two indices. If one index is used, it is assumed to be an array index. If two sets of indices are used, the first is assumed to an array index and the second is assumed to be a character index. Indices can also be used on functions and on expressions. An index can be a simple numeric literal or a complicated expression. Indices can also be nested.

An index is enclosed in square parentheses, [ and ]. Actually, () and {} work as well, but [] are used in the documentation to alleviate confusion. The colon, :, is the range separator while the semicolon, ;, is the element separator. For example, [1:5] represents the set of numbers [1;2;3;4;5]. An increment is optional with a range, and defaults to one (1). For example, [1:5:2] represents the set of numbers [1;3;5].

There are two special characters that are defined only within a variable index: # denotes the last element of a dimension, and * denotes the entire range of a dimension. The * character is not allowed on string variables. The # character can be used in an index expression, for example, X[#-1] is the next to last element of vector X. Refer to the table below for some examples of these special index characters.

X[#] is the last element of vector X
TS[#] is the last character of string variable TS
TA[#] is the last string of string array variable TA
TA[#][3:7] is characters 3 to 7 of the last string of TA
M[#,2:10] is elements 2 to 10 from the last row of matrix M
M[3:12,#-1] is elements 3 to 12 from the next to last column of M
X[*] is all of vector X
M[3,*] is all of row 3 from matrix M
M[*,5] is all of column 5 from M

# and * cannot be used in indices on output variables, since they would be undefined. They also cannot be used in indices on functions or in indices on expressions.

Indices on functions
Indices on expressions
Index as expression

  Variables
  Expressions