# Interpolations of a function that has NaN

**URL:** <https://discourse.julialang.org/t/interpolations-of-a-function-that-has-nan/95704>\
**Category:** Numerics\
**Tags:** question, interpolations\
**Created:** [March 7, 2023, 11:26pm UTC](https://discourse.julialang.org/t/interpolations-of-a-function-that-has-nan/95704 "2023-03-07T23:26:25Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [March 8, 2023, 12:12am UTC](https://discourse.julialang.org/t/interpolations-of-a-function-that-has-nan/95704/2 "2023-03-08T00:12:14Z")

</div>

Instead of using a matrix with NaNs, one possibility is to take the remaining valid entries as scattered points. See the example below using ScatteredInterpolation.jl.

 ![ScatteredInterpolation_matrix_with_NaN](https://global.discourse-cdn.com/julialang/original/3X/7/a/7abcf15976afb9654ab0baad16101b95b5e241b9.png)

> **ScatteredInterpolation.jl code**
>
> ```julia
> using ScatteredInterpolation, StaticArrays, Plots
> 
> A_x1 = 1:.1:10
> A_x2 = 1:.5:20
> f(x1, x2) = log(x1+x2)
> A = [f(x1,x2) for x1 in A_x1, x2 in A_x2]
> A[50, :] .= NaN
> A[:, 20] .= NaN
> 
> CI = findall(!isnan, A)
> points = SVector{2,Float64}[]
> samples = Float64[]
> for ci in CI
> push!(points, SA[A_x1[ci[1]], A_x2[ci[2]]])
> push!(samples, A[ci])
> end
> points = reduce(hcat, points)
> 
> nx1, nx2 = length(A_x1), length(A_x2)
> X1, X2 = vec(repeat(A_x1, nx2)), vec(repeat(A_x2', nx1))
> gridPoints = [X1 X2]'
> 
> itp = ScatteredInterpolation.interpolate(Multiquadratic(), points, samples)
> interpolated = evaluate(itp, gridPoints)
> gridded = reshape(interpolated, nx1, nx2)
> 
> plot(heatmap(A), heatmap(gridded))
> 
> ```

The packages GMT.jl and DIVAnd.jl are also very good for this task: [see this other post](https://discourse.julialang.org/t/plot-3d-data-in-a-2d-contour-plot/71251).

---

_[View the full topic](https://discourse.julialang.org/t/interpolations-of-a-function-that-has-nan/95704)._
