Programmer Guide > Using Subscripts with Arrays > Arrays as Subscripts to Other Arrays
  

Arrays as Subscripts to Other Arrays
Arrays may be used to subscript other arrays. Each element in the array used as a subscript selects an element in the subscripted array. When used with subscript ranges, more than one element is selected for each subscript element.
If no subscript ranges are present, the length and structure 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, S, which is a vector, the process may be written as:
assuming that the array A has n elements, and S has m elements. The result A(S) has the same structure and number of elements as does the subscript vector S. Just as with scalar subscripts and range subscripts, array subscripts can represent one-dimensional indices into multidimensional arrays; thus, the dimensionality of A is arbitrary.
If an element of the subscript array is less than or equal to zero, the first element of the subscripted variable is selected. If an element of the subscript array is greater than or equal to the last subscript in the subscripted variable (N, above), the last element is selected.
Example
A = [6, 5, 1, 8, 4, 3]
B = [0, 2, 4, 1, -1, 10]
C = A(B)
PRINT, C
6   1   4   5   6   3
The first element is 6 because it is in the zero position of A. The second is 1 because the value in B of 2 indicates the third position in A, and so on. The last two elements of C are the endpoints of A, because the last two subscripts of B are out of range.
As another example, assume the variable A is a 10-by-10 array. The expression:
A(INDGEN(10) * 11)
yields a 10-element vector equal to the diagonal elements of A. The one dimensional subscripts of the diagonal elements, A0,0, A1,1, ..., A9,9 are 0, 11, 22, ..., 99 (the same as elements of the vector INDGEN(10) * 11).
The WHERE function, which returns a vector of subscripts, may 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.

Version 2017.0
Copyright © 2017, Rogue Wave Software, Inc. All Rights Reserved.