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 Array 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.

If the subscript expression applied to the variable is an array and an array appears on the right side of the statement:

Variable[Array] = Array

then elements from the right side are stored in the elements designated by the subscript vector. Only those elements of the subscripted variable whose subscripts appear in the subscript vector are changed. Note the use of array subscripts (double brackets). For example, the statement:

B[[ 2, 4, 6 ]] = [4, 16, 36]

is equivalent to the following series of assignment statements:

B[2] = 4
B[4] = 16
B[6] = 36

For another example, consider the statements:

A = [6, 5, 1, 8, 4, 3]
B = [0, 2, 4, 1]
C = A[B]

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.

Subscript elements are interpreted as if the subscripted variable is a vector. For example, if A is a 10 x n matrix, the element A[i, j] has the subscript i + 10*j.

When there is an array expression on the right, it is inserted into the array appearing on the left side of the equal sign starting at the point designated by the scalar subscript. For example, the following creates intArr, a 5 column by 2 row integer array of zeros. Insert array B into intArr beginning at the position designated by the scalar subscript (note the use of single brackets).

A = INTARR(5,2)
B = [222, 333, 444]
A[1] = B
0     222     333     444       0
0       0       0       0       0

Note: The subscript array is converted to longword type before use if necessary. Regardless of structure, this subscript array is interpreted as a vector.

Array Subscripts and Clipping

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.

Note: Because of the confusion between the clipping of array subscripts and the use of negative indices (see the next section), it is best to avoid using code that relies on this clipping behavior.

Negative Indices versus Array Subscripts

As shown above, for array subscripts, negative elements are clipped to 0. However, for subscript ranges, negative indices may be used to index from the end of the array. For example:

print, A[[-2,-1,0]]

IDL prints:

0   0   0

Now try the following:

print, A[-2], A[-1], A[0]

IDL prints:

3   4   0

In the first case, since an array was used for indexing, then IDL clips all three elements of the subscript array to subscript 0. In the second case, since scalar elements were used, IDL treats these as subscript ranges, and indexes from the end of the array.

Examples Using Arrays as Subscripts

One way to create a square n x nidentity matrix is as follows:

A[INDGEN(N) * (N + 1)] = 1.0

The expression INDGEN(N)*(N + 1) results in a vector containing the subscripts of the diagonal elements [0, N+1, 2N+2, ..., (N-1)*(N+1)]. The following statements create a 10x10 identity matrix:

A = FLTARR(10, 10)
A[INDGEN(10) * 11] = 1.0

Yet another way is to use two array subscripts. The statements:


create the array subscripts [[0,0], [1,1], ..., [n-1, n-1]].

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.