Advanced Array Indexing
- A scalar index or the start
and stop of an index range may be non-positive to reference the
elements near the end of a dimension. Hence, 0 refers to the final element,
-1 refers to the next to last element, -2 to the element before that, and
so on. For example,
x(2:-1) refers to all but the first
and last elements of the 1-D array x. This convention does NOT
work for an index list.
- A range function ifunc may
be followed by a colon and an index range start:stop or start:stop:step
in order to restrict the indices to which the range function applies to
a subset of the entire dimension. Hence, x(min:2:-1) returns
the minimum of all the elements of the 1-D array x, excluding the
first and last elements.
- An index specified as a scalar,
the start or stop of an index range, or an element of
an index list may exceed the length of the indexed dimension ID,
provided that the entire indexing operation does not overreach the bounds
of the array. Thus, if yis a 5-by-6 array, then y(22)
refers to the same datum as y(2,5).
- The expression z(..)
- using the rubber-index operator .. - refers to the entire array
This is occasionally useful as the left hand side of an assignment statement
in order to force broadcasting and type conversion of the right hand expression
to the preallocated type and shape z.
- The expression z(*)
- using the rubber-index operator * - collapses a multidimensional
array z into a one-dimensional array. Even more useful as z(*,)
to preserve the final index of an array and force a two-dimensional result.