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

  • Thread starter jenny.genders
  • Start date
  • Tags
    Array Loop
In summary, the conversation discusses a loop through a large amount of data and a calculation that is only supposed to be done if the number is not equal to NaN. However, the loop is still performing the calculation for all numbers, even those that are NaN. The solution is to use the float != float test, making sure to add the "volatile" qualifier to the variables declaration to avoid compiler optimization.
  • #1
jenny.genders
1
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
  • #3
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:
 
  • #4
Good point. Add the "volatile" qualifier to the variables declaration.
 
  • #5


To check for NaN in an array, you can use the IDL function "isnan" which returns a boolean array indicating which elements in the array are NaN. You can then use this boolean array as a mask to only perform the calculation on the elements that are not NaN.

Your loop would look something like this:

for i = 0, 611 do begin
for j = 0, 2591 do begin
if ~isnan(sfc_data[j, i]) then begin
weights = weights + lat_matrix[j, 0]
endif
endfor
endfor

This way, the calculation will only be performed if the element in sfc_data is not NaN. You can also use the "isfinite" function to check for both NaN and infinity values.

Additionally, you can also use the IDL function "where" to find the indices of the elements that are not NaN and use those indices to perform the calculation. This can be more efficient for larger arrays.

for i = 0, 611 do begin
indices = where(~isnan(sfc_data[*, i]))
weights = total(lat_matrix[indices, 0])
endfor

I hope this helps!
 

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

1. What is an NaN and why would I need to check for it in an array?

An NaN, or "Not a Number", is a special value that represents an undefined or unrepresentable numerical value in a computer program. You would need to check for it in an array because it can indicate an error or missing data, which could affect the accuracy of your calculations.

2. How do I check for an NaN in an array in IDL?

To check for an NaN in an array in IDL, you can use the "isnan" function. This function takes in an array as an argument and returns a boolean array where True indicates that the corresponding element in the original array is an NaN.

3. Can I check for an NaN in an array using a loop?

Yes, you can check for an NaN in an array using a loop. You can use a "for" or "while" loop to iterate through the array and use the "isnan" function to check each element.

4. What should I do if I find an NaN in my array?

If you find an NaN in your array, you should handle it according to your specific program needs. You may choose to ignore it, replace it with a different value, or handle it as an error and terminate the program.

5. Are there any other functions or methods in IDL that can check for an NaN in an array?

Yes, there are other functions and methods in IDL that can check for an NaN in an array, such as "finite", which checks for finite values (non-NaN and non-infinite values), and "isfinite", which checks for finite values and returns a boolean array. Additionally, many built-in mathematical functions in IDL will automatically ignore NaN values in an array.

Similar threads

  • Programming and Computer Science
2
Replies
66
Views
4K
  • Programming and Computer Science
Replies
4
Views
4K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
1
Views
4K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
28
Views
3K
  • Engineering and Comp Sci Homework Help
2
Replies
43
Views
4K
  • Programming and Computer Science
Replies
20
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
Replies
11
Views
2K
Back
Top