Conditionally Altering Array Elements
The WHERE function can be used to select array elements that meet certain conditions. For example, the statement:
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:
The following statement sets elements of A with values of zero or less to -1:
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:
- We use the LT (less than) operator to create an array, with the same dimensions as data, that contains a 1 for every element of data that is less than zero and a zero for every element of data that is zero or greater. We use this "drop-out array" as a parameter for the WHERE function, which generates a vector that contains the one-dimensional subscripts of the elements of the drop-out array that are nonzero. The resulting vector, stored in the variable
bad, contains the subscripts of the elements of data that are less than zero. - The expression
data[bad - 1]is a vector that contains the subscripts of the points immediately preceding the drop-outs; while similarly, the expressiondata[bad + 1]is a vector containing the subscripts of the points immediately after the drop-outs. - The average of these two vectors is stored in
data[bad], the points that originally contained drop-outs.
Note
Also see Example—Using Array Operators and WHERE for an additional example.