Arrays and Efficient Programming
IDL has been specifically designed to process arrays easily and naturally. You can get excellent performance in your applications by using the built-in array processing routines, which allow an operation to be performed on every element in an array, without having to explicitly create a loop. This functionality makes for simpler coding and faster computing. For more information, see Writing Efficient IDL Programs.
Using Operators on Arrays
IDL has a large number of different operators. In addition to the usual operators — addition, subtraction, multiplication, division, exponentiation, relations (EQ, NE, GT, etc.), and logical arithmetic (&&, ||, ~, AND, OR, NOT, and XOR) — other operators exist to find minima, maxima, select scalars and subarrays from arrays (subscripting), and to concatenate scalars and arrays to form new arrays.
All of IDL's operators can be applied to arrays as well as to scalars.
IDL's ability to perform operations directly on entire arrays makes it ideal for processing array data. For example, suppose you had an array A consisting of 100 floating point integers, and you wanted to create a corresponding array containing the absolute value of each array element. Most languages would require you to write a loop to create the new array. In IDL, the following single statement suffices:
The array B is created as a 100-element array, each element of which contains the absolute value of the corresponding element in array A.
Similarly, multiplying each element of array C by the corresponding element of array D is simple:
See Arrays and Expressions and Operators for additional details.
Subscripts
Subscripts retrieve or modify individual array elements, and are also referred to as array indices. In IDL subscripts, the first array index element is always zero. This is different from FORTRAN, where indices start by default with one. In a one-dimensional array, 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.
You can use array subscripts to access one element, a range of elements, or a number of non-sequential elements in an array. You can also use subscripts to designate new values for array elements.
For example, the following expression gives the value of the seventh element of the variable arr (remember that array subscripts start at zero, not 1).
The next statement stores the number three at the seventh element of arr, with no changes to other array elements.
Using Array Operators to Avoid IF Statements
Suppose you want to add all positive elements of array B to array A:
Using Array Operators and the WHERE Function
In the example below, each element of the array C is set to the square-root of the corresponding element of array A if A[i] is positive; otherwise, C[i] is set to minus the square-root of the absolute value of A[i].
Another method is to use the WHERE function to determine the subscripts of the negative elements of A and negate the corresponding elements of the result.
Using Vector and Array Operations
Whenever possible, vector and array data should be processed with IDL array operations instead of scalar operations in a loop. For example, consider the problem of flipping a 512 × 512 image. This problem arises because approximately half the available image display devices consider the origin to be the lower-left corner of the screen, while the other half recognize it as the upper-left corner.
Note
The following example is for demonstration only. The IDL system variable !ORDER and corresponding features in the iTools and Direct graphics image display routines are easier to use and more efficient.
A programmer without experience in using IDL might be tempted to write the following nested loop structure to solve this problem:
A more efficient approach to this problem capitalizes on IDL's ability to process arrays as a single entity.
At the cost of using twice as much memory, processing can be simplified even further by using the following statements:
that reverses the array using subscript ranges and array-valued subscripts.