# Error using isnan

**URL:** <https://discourse.julialang.org/t/error-using-isnan/20798>\
**Category:** General Usage\
**Created:** [February 14, 2019, 10:31pm UTC](https://discourse.julialang.org/t/error-using-isnan/20798 "2019-02-14T22:31:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Erisme](https://avatars.discourse-cdn.com/v4/letter/e/96bed5/32.png) [@Erisme](https://discourse.julialang.org/u/Erisme)\
**Post date:** [February 14, 2019, 10:31pm UTC](https://discourse.julialang.org/t/error-using-isnan/20798/1 "2019-02-14T22:31:04Z")

</div>

Hi,

I have trouble using the function isnan. I want to replace NaN value by 0. any help?  
julia\> a=[1 2 3; 4 5 NaN]  
2×3 Array{Float64,2}:  
1.0 2.0 3.0  
4.0 5.0 NaN

julia\> a[isnan(a)]=0  
ERROR: MethodError: no method matching isnan(::Array{Float64,2})  
Closest candidates are:  
isnan(::BigFloat) at mpfr.jl:879  
isnan(::Missing) at missing.jl:83  
isnan(::Float16) at float.jl:530  
…  
Stacktrace:  
[1] top-level scope at none:0

julia\>

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [February 14, 2019, 10:39pm UTC](https://discourse.julialang.org/t/error-using-isnan/20798/2 "2019-02-14T22:39:32Z")

</div>

`isnan` is a function defined for scalar variables, you have to broadcast it to all elements of the array:

```julia
a[isnan.(a)] .= 0

```

or you can use the `@.` macro to dot everything for you:

```julia
@. a[isnan(a)] = 0

```

---

<div class="post-metadata">

**Author:** ![Erisme](https://avatars.discourse-cdn.com/v4/letter/e/96bed5/32.png) [@Erisme](https://discourse.julialang.org/u/Erisme)\
**Post date:** [February 14, 2019, 10:53pm UTC](https://discourse.julialang.org/t/error-using-isnan/20798/3 "2019-02-14T22:53:39Z")

</div>

Thanks very much it works

---

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [February 15, 2019, 1:46am UTC](https://discourse.julialang.org/t/error-using-isnan/20798/4 "2019-02-15T01:46:46Z")

</div>

I think `replace!` is a much better fit for this purpose. It is so fast and has zero allocations.

```julia
julia> @btime replace!($a, NaN => 0)
  8.980 ns (0 allocations: 0 bytes)
2×3 Array{Float64,2}:
 1.0 2.0 3.0
 4.0 5.0 0.0

```
