Skip to main content

4.6) where Function


The WHERE function allows you to find the subscripts of the elements of an array whose contents satisfy a specified criteria. WHERE returns a vector which contains the one-dimensional subscripts of the elements, which can then be used to subset the original array. A WHERE statement takes the form:

result = WHERE(criteria, count)

"Criteria" is a test to be performed on an array of data using the same logical operators used in the IF statement and count gives the number of elements which satisfy the criteria. For example:

x = indgen(10)
y= WHERE(x gt 5, count)

In this case, y will be a vector containing the array subscripts for which x is greater than 5. We can then use y to reference x to get these values of x which are greater than 5.

xgt5 = x(y)
print, xgt5

IDL will print "6 7 8 9" to the screen and the value of count will be 4.