2
votes

How can I construct an interpolate of unevenly spaced data in Julia, using nearest neighbor interpolation? I took a look at the documentation and typed the following at REPL:

using Interpolations
input = [1.0 60; 1.1 0; 2.0 60; 2.3 0; 4.0 430; 4.05 0]
itp   = interpolate(input[:,1], input[:,2], Gridded(Constant()))

Which seemed straightforward to me, but gives:

ERROR: LoadError: MethodError: no method matching interpolate(::Array{Float64,1}, ::Array{Float64,1}, ::Gridded{Constant})

Do I need to convert the Arrays to Vectors? If so, how? Please tell me there is an easy solution...

1

1 Answers

4
votes

When you are in an N-dimensional space, you should put the coordinates of interpolation "knots" in an N-tuple of vectors. In 1-D, this means a 1-tuple like (x,) instead of plain x:

julia> input = [1.0 60; 1.1 0; 2.0 60; 2.3 0; 4.0 430; 4.05 0]
6×2 Array{Float64,2}:
 1.0    60.0
 1.1     0.0
 2.0    60.0
 2.3     0.0
 4.0   430.0
 4.05    0.0

julia> x = input[:, 1];
julia> y = input[:, 2];
julia> itp = interpolate((x,), y, Gridded(Constant()))
6-element interpolate((::Array{Float64,1},), ::Array{Float64,1}, Gridded(Constant())) with element type Float64:
  60.0
   0.0
  60.0
   0.0
 430.0
   0.0

julia> itp(1.01)
60.0