Interpolations of a function that has NaN

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.jl code
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.

4 Likes