# Matching for interpolation

**URL:** <https://discourse.julialang.org/t/matching-for-interpolation/103636>\
**Category:** General Usage\
**Tags:** match, intervals, pattern-matching, mlstylejl\
**Created:** [September 7, 2023, 11:42pm UTC](https://discourse.julialang.org/t/matching-for-interpolation/103636 "2023-09-07T23:42:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lincoln\_Hannah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lincoln_hannah/32/19198_2.png) [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)\
**Post date:** [September 7, 2023, 11:42pm UTC](https://discourse.julialang.org/t/matching-for-interpolation/103636/1 "2023-09-07T23:42:37Z")

</div>

Is there a matching package that can do something like

```julia
V = [1.1, 2, 4.8, 6, 9, 11]
x = 5.1

@match begin

     V[i] ≤ x < V[i+1] => #some interp function of V[i] and V[i+i]

```

---

<div class="post-metadata">

**Author:** ![skleinbo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skleinbo/32/36080_2.png) [@skleinbo](https://discourse.julialang.org/u/skleinbo)\
**Post date:** [September 8, 2023, 7:24am UTC](https://discourse.julialang.org/t/matching-for-interpolation/103636/2 "2023-09-08T07:24:30Z")

</div>

Not that I know of, but you can simply use `i = findfirst(>=(x), V)` to figure out the indices, given that `V` is sorted. I guess the rest depends on what kind of interpolation you are looking for. For example a piecewise linear interpolation over the full range can be constructed efficiently with `DataInterpolations.jl`. Guess I am misunderstanding your aim.

---

<div class="post-metadata">

**Author:** ![Lincoln\_Hannah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lincoln_hannah/32/19198_2.png) [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)\
**Post date:** [September 8, 2023, 8:07am UTC](https://discourse.julialang.org/t/matching-for-interpolation/103636/3 "2023-09-08T08:07:19Z")

</div>

Thanks. yes I could use an interpolation package.  
This is for the purpose of sharing the code with others.  
I’m wondering how close an interpolation algorithm can look to the math document describing it.

---

<div class="post-metadata">

**Author:** ![skleinbo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skleinbo/32/36080_2.png) [@skleinbo](https://discourse.julialang.org/u/skleinbo)\
**Post date:** [September 8, 2023, 8:52am UTC](https://discourse.julialang.org/t/matching-for-interpolation/103636/4 "2023-09-08T08:52:47Z")

</div>

Ah, so it’s mainly for educational purposes.

Isn’t `i = findfirst(>=(x), V)` (plus a few lines to handle corner cases) as close as it gets to the algorithm step

> Find i such that V[i]\leq x\lt V[i+1]

…?

---

<div class="post-metadata">

**Author:** ![digital\_carver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/digital_carver/32/33818_2.png) [@digital\_carver](https://discourse.julialang.org/u/digital_carver)\
**Post date:** [September 8, 2023, 2:49pm UTC](https://discourse.julialang.org/t/matching-for-interpolation/103636/5 "2023-09-08T14:49:24Z")

</div>

If the vector is known to be in sorted order, you can also use `searchsortedlast(V, x)`.

> searchsortedlast(a, x; by=, lt=, rev=false)
> 
> Return the index of the last value in a less than or equal to x, according  
> to the specified order. Return firstindex(a) - 1 if x is less than all  
> values in a. a is assumed to be sorted.

* * *

Something closer to the notation you’re looking for maybe:

```julia
julia> findfirst(i -> V[i] ≤ x < V[i+1], eachindex(V)[1:end-1])
3

```
