Subscript Ranges
Subscript ranges are used to select a subarray from an array by giving the starting and ending subscripts of the subarray in each dimension. Subscript ranges can be combined with scalar and array subscripts and with other subscript ranges. Any rectangular portion of an array can be selected with subscript ranges.
Note
Processing subscript ranges is inefficient. When possible, use an array or scalar subscript instead of specifying a subscript range where the beginning and ending subscripts are separated by the colon character. See Avoid Using Range Subscripts for details.
There are six types of subscript ranges:
Multidimensional subarrays can be specified using any combination of the above forms. For example, if arr is a 10x10 array, arr[*, 0:4] is made from all columns of rows 0 to 4 of arr or a 10-column, 5-row array.
Dimensionality of Subarrays
The dimensions of an extracted subarray are determined by the size in each dimension of the subscript range expression. In general, the number of dimensions is equal to the number of subscripts and subscript ranges. The size of the n-th dimension is equal to one if a simple subscript was used to specify that dimension in the subscript; otherwise, it is equal to the number of elements selected by the corresponding range expression.
Degenerate dimensions (trailing dimensions with a size of one) are removed. If arr is a 10-column by 12-row array, the expression arr[*,11] results in a row vector with a single dimension. (The result of the expression is a 10-column by 1-row array; the last dimension is degenerate and is removed.) On the other hand, the expression arr[0, *] became a column vector with dimensions of [1, 12], showing that the structure of columns is preserved because the dimension with a size of one does not appear at the end.
To see this, enter the following statements in IDL:
In the following examples, vec is a 50-element floating-point vector, and arr is a 10-column by 12-row integer array. Some typical subscript range expressions are as follows:
vec = FINDGEN(50) arr = INDGEN(10,12) ; Elements 5 through 10 of vec, a six-element vector. vec[5:10] ; A three-element vector. vec[I - 1:I + 1] ; The same vector. [vec[I - 1], vec[I], vec[I + 1]] ; Elements from vec[4] to the end, a 46-element (50-4) vector. vec[4:*] ; Values of the elements with even subscripts in vec. vec[0:*:2] ; Values of the elements with odd subscripts in vec: vec[1:*:2] ; The fourth column of arr, a 1 column by 12 row vector. arr[3, *] ; The first row of arr, a 10-element row vector. Note, the last ; dimension was removed because it was degenerate. [arr[3, 0], arr[3, 1], ..., arr[3, 11]] arr[*, 0] ; The nine-point neighborhood surrounding arr[X,Y], a 3 by 3 array. arr[X - 1:X + 1, Y - 1:Y + 1] ; Three columns of arr, a 3 by 12 subarray: arr[3:5,*]
To insert the contents of an array called A into array B, starting at point B[13, 24], use the following statement:
If A is a 5-column by 6-row array, elements B[13:17, 24:29] are replaced by the contents of array A.
In the next example, a subarray is moved from one position to another:
A subarray of B, specifically the columns 200 to 300 and rows 300 to 400, is moved to columns 100 to 200 and rows 200 to 300, respectively.
Assuming the variable B is a 512 × 512-byte array, some examples are as follows:
; Store 1 in every element of the i-th row. array[*, I] = 1 ; Store 1 in every element of the j-th column. array[J, *] = 1 ; Zero all the rows of columns 200 through 220 of array. array[200:220, *] = 0 ; Store the value 100 in all the elements of array. array[*] = 100