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

log*_*ick 10 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
Run Code Online (Sandbox Code Playgroud)

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

gio*_*ano 14

使用isequal

与 类似==,但处理浮点数和缺失值除外。isequal将所有浮点NaN值视为彼此相等,视为-0.0不等于0.0,并missing视为等于missing。总是返回一个Bool值。

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
Run Code Online (Sandbox Code Playgroud)