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

  • Thread starter Thread starter jenny.genders
  • Start date Start date
  • Tags Tags
    Array Loop
Click For Summary
The discussion focuses on a coding issue involving a loop that processes a 612x2592 data array, specifically checking for NaN values in the sfc_data array. The user intends to perform calculations with another array, lat_matrix, only when the corresponding sfc_data element is a valid number. However, the current implementation incorrectly executes calculations regardless of NaN presence. The solution involves recognizing that in floating-point comparisons, NaN is unique in that it does not equal itself, leading to the suggestion to use the condition x != x for checks. Additionally, to prevent compiler optimization from disregarding this check, it is advised to declare variables with the "volatile" qualifier.
jenny.genders
Messages
1
Reaction score
0
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?
 
Technology news on Phys.org
TylerH said:
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:
 
Good point. Add the "volatile" qualifier to the variables declaration.
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 1 ·
Replies
1
Views
5K
  • · Replies 66 ·
3
Replies
66
Views
6K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 28 ·
Replies
28
Views
3K
  • · Replies 4 ·
Replies
4
Views
11K
  • · Replies 20 ·
Replies
20
Views
4K
  • · Replies 10 ·
Replies
10
Views
26K
  • · Replies 1 ·
Replies
1
Views
29K