4855 Rate this article:
5.0

Locating NaN and Inf values in an array

Mark Alonzo
(This week's post is brief because I'm out learning a lot at the 2013 NOAA Satellite Conference!) The ubiquitous WHERE function can be used to quickly locate values in an array. However, you can't directly search for the location of IEEE NaN (not a number) and Inf (infinity) values; to do so, you need the FINITE function. Here's an example. Start with a simple array:
IDL> a = findgen(5, 2)
IDL> print, a
     0.000000      1.00000      2.00000      3.00000      4.00000
      5.00000      6.00000      7.00000      8.00000      9.00000
and add NaN values at up to three random locations:
IDL> i = floor(randomu(seed, 3)*n_elements(a))
IDL> a[i] = !values.f_nan
IDL> print, a
     0.000000      1.00000      2.00000      3.00000          NaN
          NaN      6.00000      7.00000          NaN      9.00000
OK, so where are the NaNs located? By eye, I can see where they are (indices 4, 5 and 8), but to identify them programmatically, use WHERE with FINITE and "~", the logical not operator:
IDL> i_nan = where(~finite(a), /null)
IDL> print, i_nan
           4           5           8
Check!
MOST RECENT

A Technical Dive into Creating Accurate Digital Terrain Models

6/27/2018

Live Webinar | July 25 Creating accurate digital terrain models (DTMs) is essential for modeling areas of... more »

The Science, and Art, of Hyperspectral Image Analysis

6/7/2018

Harris Geospatial Solutions – June 7, 2018 Automation is being touted as the wave of the future for... more »

SIGN UP & STAY INFORMED

Sign up to receive the latest news, events, technologies and special offers.

SIGN ME UP