Next: Lists of Subscripts, Previous: Index Subscripts, Up: Subscripts [Contents][Index]
One can refer to a number of adjacent elements of an array (Arrays) by specifying in the concerned dimension(s) a range of coordinates rather than a single one (as in Coordinate Subscripts). Such a range subscript consists of a range variable or a range expression (Ranges) with some modifications. Firstly, parentheses around the range expression are not needed when they are used as a subscript. Secondly, two more optional fields are added to a range expression in a subscript.
The most general range subscript looks like this:
r1:r2:+:>d
r1
and r2
are the range’s start and end coordinates, and may
be replaced by a single range variable. The plus sign in the optional
third field indicates that all elements in the range must be added up,
and only the result is returned. The optional fourth field indicates
that the extracted range must be moved to dimension number d
(starting at zero) in the result.
Regard the following examples, with x
a 3-by-4 array:
x(2:3,2) x(*,2:*) x(+,2) x(>1,>0)
The first example extracts the elements with coordinates (2,2)
and (3,2)
, i.e. a 2-element one-dimensional array. The second
example extracts all elements with their second coordinate equal to 2 or
3, i.e. a 3-by-2 array. The third example adds all elements with their
second coordinate equal to 2 and returns the result, which is a scalar.
The last example moves dimension number 1 to dimension number 0 and vice
versa: It transposes the array.
You cannot specify both the summation (3rd) and redirection (4th) fields, since the summation flagged by the plus sign in the third field removes the corresponding dimension, which can then no longer be redirected. If you want to sum or redirect all elements in a particular dimension, then you don’t need to specify the first two fields.
Range subscripts can be applied to arrays, file arrays (File Arrays), and strings (Strings), except that file arrays don’t support the summation field, and strings only support the range start and end fields.
Next: Lists of Subscripts, Previous: Index Subscripts, Up: Subscripts [Contents][Index]