IDL Q: How do I check for an NaN in an array, in a loop?

  • Thread starter jenny.genders
  • Start date
  • Tags
    Array Loop
  • #1
So, I'm doing a loop through 612 rows of data, each row having 2592 numbers. If the number I'm looking at is NOT equal to NaN, I want to do a calculation using a different array. This way, if the element in sfc_data is not a number, the number in lat_matrix in that same column is ignored, and if the element in sfc_data IS a number, the matching number from lat_matrix is added to the weights array. However when I do the following loop, it's doing the calculation regardless of whether the number in the first array equals NaN.

for i = 0,611 do begin
for j = 0, 2591 do begin
if sfc_data[j,i] ne 'NaN' then weights = weights + lat_matrix[j,0]
endfor
endfor

The loop is doing the full number of calculations as if none of the numbers in sfc_data are NaN (there are definitely many NaN's in sfc_data). So how do I check if sfc_data[j,i] is equal to the string NaN and isn't actually a number?
 
  • #3
In short, float != float, iff float is NaN.

But make sure that a not-very-clever compiler doesn't optimize out the test x != x, because it thinks the result will always be false :rolleyes:
 
  • #4
Good point. Add the "volatile" qualifier to the variables declaration.
 

Suggested for: IDL Q: How do I check for an NaN in an array, in a loop?

Replies
32
Views
1K
Replies
6
Views
699
Replies
6
Views
598
Replies
8
Views
550
Replies
2
Views
893
Replies
4
Views
704
Back
Top