yorick banner

Home

Manual

Packages

Global Index

Keywords

Quick Reference

Back: Scalar Index Forward: Nil Index   FastBack: Interpolating Up: Indexing FastForward: Sorting         Top: Yorick Contents: Table of Contents Index: Concept Index About: About this document

2.3.2 Selecting a range of indices

You can refer to several consecutive array elements by an index range: x(3:6) means the four element subarray [x(3), x(4), x(5), x(6)].

Occasionally, you also want to refer to a sparse subset of an array; you can add an increment to an index range by means of a second colon: x(3:7:2) means the three element subarray [x(3), x(5), x(7)].

A negative increment reverses the order of the elements: x(7:3:-2) represents the same three elements, but in the opposite order [x(7), x(5), x(3)]. The second element mentioned in the index range may not actually be present in the resulting subset, for example, x(7:2:-2) is the same as x(7:3:-2), and x(3:6:2) represents the two element subarray [x(3), x(5)].

Just as the increment defaults to 1 if it is omitted, the start and stop elements of an index range also have default values, namely the first and last possible index values. Hence, if x is a one-dimensional array with 10 elements, x(8:) is the same as x(8:10). With a negative increment, the defaults are reversed, so that x(:8:-1) is the same as x(10:8:-1).

A useful special case of the index range default rules is x(::-1), which represents the array x in reverse order.

Beware of a minor subtlety: x(3:3) is not the same thing as x(3). An index range always represents an array of values, while a scalar index represents a single value. Hence, x(3:3) is an array with a single element, [x(3)].