# Nearest Neighbour Interpolation with Interpolations.jl

**URL:** <https://discourse.julialang.org/t/nearest-neighbour-interpolation-with-interpolations-jl/52741>\
**Category:** New to Julia\
**Created:** [January 2, 2021, 6:05pm UTC](https://discourse.julialang.org/t/nearest-neighbour-interpolation-with-interpolations-jl/52741 "2021-01-02T18:05:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mark\_Williams](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mark_williams/32/7668_2.png) [@Mark\_Williams](https://discourse.julialang.org/u/Mark_Williams)\
**Post date:** [January 2, 2021, 6:05pm UTC](https://discourse.julialang.org/t/nearest-neighbour-interpolation-with-interpolations-jl/52741/1 "2021-01-02T18:05:06Z")

</div>

I’ve been using the Interpolations.jl package quite happily for a while now. I almost exclusively make use of the convienience constructors for both linear and cubic (mostly 1d) interpolation. I’ve only just recently come accross an aplication whereby I need to use nearest neighbour interpolation, however I just can’t get it to work. Can anyone she any light on what I’m doing wrong. Sorry for presenting such a pedestrian question!

```julia
using Interpolations
using Plots

a = range(0,2*pi,length=8);
f(x) = cos(x);

x = range(0,2*pi,length=100);

linear = LinearInterpolation(a, f.(a), extrapolation_bc = Line());
cubic = CubicSplineInterpolation(a, f.(a), extrapolation_bc = Line());
nearest = interpolate(a, BSpline(Constant()));

plt = plot(x, f.(x), label="Cosine")
plt = plot!(x, linear.(x), label="Linear Interp")
plt = plot!(x, cubic.(x), label="Cubic Interp")
plt = plot!(x, nearest.(x), label="Nearest")
plt = plot!(title="Interpolation Schemes", legend = :bottomleft)

```

This throws a bounds error. The example works fine for linear and cubic interpolation. Many thanks.

---

<div class="post-metadata">

**Author:** ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)\
**Post date:** [January 2, 2021, 7:10pm UTC](https://discourse.julialang.org/t/nearest-neighbour-interpolation-with-interpolations-jl/52741/2 "2021-01-02T19:10:23Z")

</div>

I think you want to use [Scaled BSplines](https://juliamath.github.io/Interpolations.jl/latest/control/#Scaled-BSplines-1).

```julia
linear = scale(interpolate(f.(a), BSpline(Linear())), a)
cubic = scale(interpolate(f.(a), BSpline(Cubic(Line(OnGrid())))), a)
nearest = scale(interpolate(f.(a), BSpline(Constant())), a)

```

---

<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:** [January 2, 2021, 7:38pm UTC](https://discourse.julialang.org/t/nearest-neighbour-interpolation-with-interpolations-jl/52741/3 "2021-01-02T19:38:09Z")

</div>

This seems to work (see [SO](https://stackoverflow.com/questions/59992980/how-to-interpolate-data-from-unevenly-spaced-array-in-julia)):

```julia
nearest = interpolate((a,), f.(a), Gridded(Constant()))

```

![nearest_interpolation](https://global.discourse-cdn.com/julialang/original/3X/a/f/af4c2073cbff78b71d7b16359c29ee2c819f7a95.png)

---

<div class="post-metadata">

**Author:** ![Mark\_Williams](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mark_williams/32/7668_2.png) [@Mark\_Williams](https://discourse.julialang.org/u/Mark_Williams)\
**Post date:** [January 2, 2021, 10:23pm UTC](https://discourse.julialang.org/t/nearest-neighbour-interpolation-with-interpolations-jl/52741/4 "2021-01-02T22:23:15Z")

</div>

Perfect, thanks. I tried the same but couldn’t get it to work. Probably a syntax related issue,
