IDL Q: How do I check for an NaN in an array, in a 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?
 

Answers and Replies

  • #3
AlephZero
Science Advisor
Homework Helper
6,994
292
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
724
0
Good point. Add the "volatile" qualifier to the variables declaration.
 

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

Replies
1
Views
3K
  • Last Post
Replies
0
Views
2K
Replies
1
Views
2K
  • Last Post
Replies
1
Views
2K
Replies
3
Views
2K
Replies
2
Views
3K
Replies
10
Views
7K
  • Last Post
Replies
11
Views
4K
Replies
3
Views
3K
Top