Overview of Arrays

Arrays are multidimensional data sets which are manipulated according to mathematical rules. An array can be of any IDL data type; saying that an array is of a particular type means that all elements of the array are of that data type. Array subscripts provide a means of selecting one or more elements of an array for retrieval or modification.

One-dimensional arrays are often called vectors. The following IDL statement creates a vector with five single-precision floating-point elements:

array = [1.0, 2.0, 3.0, 4.0, 5.0] 

Two-dimensional arrays are often used in image processing and in mathematical operations (where they are often called matrices). The following IDL statement creates a three-column by two-row array:

array = [[1, 2, 3], [4, 5, 6]] 

Use the PRINT procedure to display the contents of the array:

PRINT, array 

IDL prints:

       1       2       3 
       4       5       6 

Arrays can have up to eight dimensions in IDL. The following IDL statement creates a three-column by four-row by five-layer deep three-dimensional array. In this case, we use the IDL FINDGEN function to create an array whose elements are set equal to the floating-point values of their one-dimensional subscripts:

array = FINDGEN(3, 4, 5) 

IDL is an array-oriented language. This means that any operation on an array is performed on all elements of the array, without the need for the user to write an explicit loop. The resulting code is easier to read and understand, and executes more efficiently. For example, suppose you have a three-dimensional array and wish to divide each element by two. A language that does not support array operations would require you to write a loop to perform the division for each element; IDL can accomplish the division in a single line of code:

array = array/2 

Determining the Number of Array Elements

The N_ELEMENTS function returns the number of elements contained in any expression or variable. Scalars always have one element. The number of elements in arrays or vectors is equal to the product of the dimensions. The N_ELEMENTS function returns zero if its parameter is an undefined variable. The result is always a longword scalar. For example, the following expression is equal to the mean of a numeric vector or array.

array = FINDGEN(3, 4, 5) 
PRINT, TOTAL(array) / N_ELEMENTS(array) 

Operations on Array Expressions

Functions exist to create arrays of the data types IDL supports. (See "Array Creation" (IDL Quick Reference) for a list of available routines.) The dimensions of the desired array are the parameters to these functions. The result of FLTARR(5) is a floating-point array with one dimension, a vector, with five elements initialized to zero. FLTARR(50,100) is a two-dimensional array, a matrix, with 50 columns and 100 rows.

The size of an array-valued expression is equal to the smaller of its array operands. For example, adding a 50-point array to a 100-point array gives a 50-point array; the last 50 points of the larger array are ignored. Array operations are performed point-by-point, without regard to individual dimensions. An operation involving a scalar and an array always yields an array of identical dimensions. When two arrays of equal size (number of elements) but different dimensionality are operands, the result is of the same dimensionality as the first operand. For example:

; Yields fltarr(4). 
FLTARR(4) + FLTARR(1, 4) 

In the above example, a row vector is added to a column vector and a row vector is obtained because the operands are the same size. This causes the result to take the dimensionality of the first operand. Here are some examples of expressions involving arrays:

; An array in which each element is equal to the same element in  
; ARR plus one. The result has the same dimensions as ARR. If ARR  
; is byte or integer, the result is of integer type; otherwise, the  
; result is the same type as ARR. 
ARR + 1 
 
; An array obtained by summing two arrays. 
ARR1 + ARR2 
 
; An array in which each element is set to twice the smaller of  
; either the corresponding element of ARR or 100. 
(ARR < 100) * 2 
 
; An array in which each element is equal to the exponential of the  
; same element of ARR divided by 10. 
EXP(ARR/10.) 
 
; An inefficient way of coding ARR * (3./MAX(ARR)) 
ARR * 3./MAX(ARR) 

In the last example, each point in ARR is multiplied by three, then divided by the largest element of ARR. The MAX function returns the largest element of its array argument. This way of writing the statement requires that each element of ARR be operated on twice. If (3./MAX(ARR)) is evaluated with one division and the result then multiplied by each point in ARR, the process requires approximately half the time.

Array Subscripts

Subscripts are used to select individual elements of an array for retrieval or modification. The subscript of an array element denotes the address of the element within the array. In the simple case of a one-dimensional array (that is, an n-element vector), elements are numbered starting at 0 with the first element, 1 for the second element, and running to n - 1, the subscript of the last element.

The syntax of a subscript reference is:

Variable_Name [Subscript_ List]

or

(Array_Expression)[Subscript_List]

The Subscript_List is simply a list of expressions, constants, or subscript ranges containing the values of one or more subscripts. Subscript expressions are separated by commas if there is more than one subscript. In addition, multiple elements are selected with subscript expressions that contain either a contiguous range of subscripts or an array of subscripts. Factors affecting the outcome of the expression include whether the subscript appears on the right or left side of the assignment operator, and the dimensionality of the subscript (scalar, array or range). See the following topics for more information: