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:

Table 15-3: Subscript Range Forms 

Subscript Format
Description

[*]

All elements of a dimension.

This form is used with multidimensional arrays to select all elements along the dimension. For example, if arr is a 10-column by 12-row array, arr[*, 11] is the last row of arr, composed of elements [arr[0,11], arr[1,11], ..., arr[9,11]], and is a 10-element row vector. Similarly, arr[0, *] is the first column of arr, [arr[0,0], arr[0,1],..., arr[0,11]], and its dimensions are 1 column by 12 rows.

[e0:e1]

Subscript range from e 0 to e 1.

This denotes all elements whose subscripts range from the expression e0 through e1 (e0 must not be greater than e1). For example, if the variable vec is a 50-element vector, vec[5:9] is a five-element vector composed of vec[5] through vec[9].

[e0:*]

A range from given element to the last element of dimension.

This denotes all elements from a given element to the last element of the dimension. If the variable vec is a 50-element vector, vec[10:*] is a 40-element vector made from vec[10] through vec[49].

[ e0:e1:e2 ]

Every e2th element in a range of subscripts from e 0 to e 1.

This denotes every e2th element within the range of subscripts e0 through e1 ( e0 must not be greater than e1). e2 is referred to as the subscript stride. The stride value must be greater than or equal to 1. If it is set to the value 1, the resulting subscript expression is identical in meaning to [e0:e1], as described above. For example, if the variable vec is a 50-element vector, vec[5:13:2] is a five-element vector composed of vec[5], vec[7], vec[9], vec[11], and vec[13].

[e0:*:e2]

Every e2th element from element e 0 to the end of dimension.

This denotes every e2th element from a given element to the last element of the dimension, written as [e0:*:e2] where e2 is referred to as the subscript stride. The stride value must be greater than or equal to 1. If it is set to the value 1, the resulting subscript expression is identical in meaning to [e0:*], as described above. If the variable vec is a 50-element vector, vec[10:*:4] is a 10-element vector made from every fourth element between vec[10] through vec[49].

[n]

A simple subscript.

When used with multidimensional arrays, simple subscripts specify only elements with subscripts equal to the given subscript in that dimension.

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:

arr = INDGEN(10,12) 
HELP, arr 
HELP, arr[*,11] 
HELP, arr[0,*] 

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:

B[13, 24] = A 

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:

B[100, 200] = B[200:300, 300:400] 

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