Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if two arrays are equal even if they contain NaN values in Julia?

Tags:

julia

I am trying to compare two arrays. It just so happens that the data for the arrays contains NaN values and when you compare arrays with NaN values, the results are not what I would have expected.

julia> a = [1,2, NaN]
3-element Array{Float64,1}:
   1.0
   2.0
 NaN  

julia> b = [1,2, NaN]
3-element Array{Float64,1}:
   1.0
   2.0
 NaN  

julia> a == b
false

Is there an elegant way to ignore these Nan's during comparison or replace them efficiently?

like image 409
logankilpatrick Avatar asked Feb 01 '20 13:02

logankilpatrick


2 Answers

Use isequal:

Similar to ==, except for the treatment of floating point numbers and of missing values. isequal treats all floating-point NaN values as equal to each other, treats -0.0 as unequal to 0.0, and missing as equal to missing. Always returns a Bool value.

julia> a = [1,2, NaN]
3-element Array{Float64,1}:
   1.0
   2.0
 NaN  

julia> b = [1,2, NaN]
3-element Array{Float64,1}:
   1.0
   2.0
 NaN  

julia> isequal(a, b)
true
like image 125
giordano Avatar answered Oct 11 '22 07:10

giordano


You probably want to use isequal(a, b) (which also treats missing equal to missing, but -0.0 as unequal to 0.0).

like image 29
Kristoffer Carlsson Avatar answered Oct 11 '22 07:10

Kristoffer Carlsson