Arrays can be used as subscripts to other arrays. Each element in the subscript array selects an element in the subscripted array. When subscript arrays are used in conjunction with subscript ranges (as discussed in Combining Subscripts), more than one element may be selected for each element of the subscript array.
If no subscript ranges are present, the length and dimensionality of the result is the same as that of the subscript expression. The type of the result is the same as that of the subscripted array. If only one subscript is present, all subscripts are interpreted as if the subscripted array has one dimension.
In the simple case of a single subscript array, the process can be described as follows:
Here, the vector V has n elements, and the subscript array S has m elements. The result V[S] has the same dimensionality and number of elements as S.
If an element of the subscript array is less than or equal to zero, the first element of the subscripted array is selected. If an element of the subscript array is greater than or equal to the last subscript in the subscripted array, the last element is selected. This clipping of out of bounds elements can be disabled within a routine by using the STRICTARRSUBS option to the COMPILE_OPT statement. (See the documentation for COMPILE_OPT for details.) If STRICTARRSUBS is in force, then array subscripts that refer to out of bounds elements will instead cause IDL to issue an error and stop execution, just as an out-of-range scalar subscript does.
As an example, consider the commands:
A = [6, 5, 1, 8, 4, 3] B = [0, 2, 4, 1] C = A[B] PRINT, C
This produces the following output:
6 1 4 5
The first element of
C is 6 because that is the number in the 0 position of
A. The second is 1 because the value in
B of 2 indicates the third position in
A, and so on.
As another example, assume the variable
A is a 10 by 10 array. Here, the subscripts of the diagonal elements (
A[0,0], A[1,1], ..., A[9, 9]) are equal to 0, 11, 22, ..., 99. The elements of the vector
INDGEN(10)*11 also are equal to 0, 11, 22, ..., 99, so the expression
A[INDGEN(10) * 11] yields a 10-element vector containing to the diagonal elements of A.
The WHERE function, which returns a vector of subscripts, can be used to select elements of an array using expressions similar to
A[WHERE(A GT 0)], which results in a vector composed only of the elements of
A that are greater than 0.