|
Application Programming: Arrays |
|
The WHERE function can be used to select array elements that meet certain conditions. For example, the statement:
data[WHERE(data LT 0)] = -1
sets all negative elements of data to -1 without changing the positive elements. The result of the function, WHERE(data LT 0), is a vector composed of the subscripts of the negative elements of data. Using this vector as a subscript changes only the negative elements.
Similarly, the WHERE function can 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.
The following statements create and display a 5x5 identity matrix, which consists of ones along a diagonal, and zeros everywhere else:
A = FLTARR(5, 5) A[INDGEN(5) * 6] = 1 PRINT, A
The following statement sets elements of A with values of zero or less to -1:
A[WHERE(A LE 0)] = -1 PRINT, A
In this example, assume that the vector data contains data elements and that a data drop-out is denoted by a negative value. In addition, assume that there are never two or more adjacent drop-outs. The following statements replace all drop-outs with the average of the two adjacent good points:
; Subscript vector of drop-outs. bad = WHERE(data LT 0) ; Replace drop-outs with average of previous and next point. data[bad] = (data[bad - 1] + data[bad + 1]) / 2
In this example, the following actions are performed:
bad, contains the subscripts of the elements of data that are less than zero.
data[bad - 1] is a vector that contains the subscripts of the points immediately preceding the drop-outs; while similarly, the expression data[bad + 1] is a vector containing the subscripts of the points immediately after the drop-outs.
data[bad], the points that originally contained drop-outs.
| Note Also see Example—Using Array Operators and WHERE for an additional example. |
IDL Online Help (March 06, 2007)