Next: , Previous: , Up: Indexing   [Contents]


2.3.4 Selecting an arbitrary list of indices

An index list is an array of index values. Use an index list to specify an arbitrary subset of an array: x([5,1,2,1]) means the four element array [x(5), x(1), x(2), x(1)]. The where function returns an index list:

list= where(x > 3.5)
y= x(list)

These lines define the array y to be the subset of the x array, consisting of the elements greater than 3.5.

Like the result of an index range, the result of an index list is itself an array. However, the index list follows a more general rule: The dimensionality of the result is the same as the dimensionality of the index list. Hence, x([[5, 1], [2, 1]]) refers to the two dimensional array [[x(5), x(1)], [x(2), x(1)]]. The general rule for index lists is:

Dimensions from the dimensions of the index list; values from the array
being indexed.

Note that the scalar index value is a special case of an index list according to this rule.

The rule applies to multi-dimensional arrays as well: If x is a five-by-nine array, then x(, [[5, 1], [2, 1]]) is a five-by-two-by-two array. And x([[5, 1], [2, 1]], 3:6) is a two-by-two-by-four array.