# Tricky interpolation issue

**URL:** <https://discourse.julialang.org/t/tricky-interpolation-issue/90142>\
**Category:** General Usage\
**Tags:** interpolations\
**Created:** [November 12, 2022, 12:55am UTC](https://discourse.julialang.org/t/tricky-interpolation-issue/90142 "2022-11-12T00:55:20Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ejlongman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ejlongman/32/36801_2.png) [@ejlongman](https://discourse.julialang.org/u/ejlongman)\
**Post date:** [November 12, 2022, 12:55am UTC](https://discourse.julialang.org/t/tricky-interpolation-issue/90142/1 "2022-11-12T00:55:21Z")

</div>

Hi All,

I am trying to create a function that turns a shorter vector (len nk) into a longer vector (len nkp). For example, 1x10 would become 1x20. I’m running into an issue with indexing. I see why mine does not work, but does anyone have ideas of alternative ways to do this?? The key is that the entries in the vector can be spaced in any way (for example exponentially) not necessarily just linearly.

```julia
function fillin(short_grid)
        long_grid = zeros(nkp)
        for i in 1:nkp
            #find the indexes on short K below and above   
            down = Int(floor((nk-1.0)*(i-1.0)/(nkp-1.0))+1)
            up = down+1

            #compare exact location 
            num = i*nk/nkp - down
            denom = short_grid[down] - short_grid[up]
            frak = num/denom

            long_grid[i] = frak*short_grid[up]+(1-frak)*short_grid[down]
        end;
    end;
    return long_grid 
 end;

kg_long = fillin(kg)

```

I get:  
BoundsError: attempt to access 11-element Vector{Float64} at index [12]

Stacktrace:  
[1] getindex(A::Vector{Float64}, i1::Int64)  
@ Base ./array.jl:861  
[2] fillin(short\_grid::Vector{Float64})  
@ Main ./In[23]:15  
[3] top-level scope  
@ In[23]:24  
[4] eval  
@ ./boot.jl:373 [inlined]  
[5] include\_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)  
@ Base ./loading.jl:1196

---

<div class="post-metadata">

**Author:** ![martin.d.maas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/martin.d.maas/32/50964_2.png) [@martin.d.maas](https://discourse.julialang.org/u/martin.d.maas)\
**Post date:** [November 12, 2022, 1:33am UTC](https://discourse.julialang.org/t/tricky-interpolation-issue/90142/2 "2022-11-12T01:33:48Z")

</div>

I couldn’t run your code (it looks like there was an error when copy-pasting)… but what exactly are you trying to do? There are many packages out there that provide interpolation routines, where you able to take a look at [Interpolations.jl](https://github.com/JuliaMath/Interpolations.jl) for example?

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [November 12, 2022, 9:33am UTC](https://discourse.julialang.org/t/tricky-interpolation-issue/90142/3 "2022-11-12T09:33:46Z")

</div>

First a general note:  
`nkp` and `nk` are not defined inside the function. My guess is, you are using global vars here, this is not recommended. Try to use them as parameters to the function. This would also help for a [MWE](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757).

Your error

> [@ejlongman](#):
>
> attempt to access 11-element Vector{Float64} at index [12]

results from accessing `short_grid[up]` and `short_grid[down]`. From your code it is not clear that `up` and `down` are really restricted to the allowed indices from 1 to 11.  
For an example with `nk=11` and `nkp=20` you get in the loop at the last `i=nkp`:

```julia
julia> nk=11
11

julia> nkp=20
20

julia> i=nkp
20

julia> down = Int(floor((nk-1.0)*(i-1.0)/(nkp-1.0))+1)
11

julia> up = down+1
12

```

Now it is clear that `short_grid[up]` should fail.

Perhaps it is already solved with

```julia
for i in 1:(nkp-1)

```

but that is just guessing.

To give you some alternative we need to know what exactly you try to do. Best would be a MWE with some example input and your desired output and some explanations on the how and why the desired result should be achieved.

---

<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:** [November 12, 2022, 6:57pm UTC](https://discourse.julialang.org/t/tricky-interpolation-issue/90142/4 "2022-11-12T18:57:59Z")

</div>

This is a very simple way to interpolate the vector from `N` elements to `2N-1`:

```julia
using Interpolations
xshort = exp.(0:9)
nk = length(xshort)
itp = LinearInterpolation(1:nk, xshort)
xlong = itp(1:0.5:nk)

```

---

<div class="post-metadata">

**Author:** ![ejlongman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ejlongman/32/36801_2.png) [@ejlongman](https://discourse.julialang.org/u/ejlongman)\
**Post date:** [November 12, 2022, 9:54pm UTC](https://discourse.julialang.org/t/tricky-interpolation-issue/90142/5 "2022-11-12T21:54:55Z")

</div>

Thanks everyone!

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [November 12, 2022, 10:21pm UTC](https://discourse.julialang.org/t/tricky-interpolation-issue/90142/6 "2022-11-12T22:21:13Z")

</div>

By the way, `LinearInterpolation` is currently deprecated in favor of `linear_interpolation` as not to imply there is a `LinearInterpolation` type.

Also please see [Home · Interpolations.jl](http://juliamath.github.io/Interpolations.jl/stable/#Performant-Example-Usage) about how to use Interpolations.jl efficiently.

If you do not need extrapolation or scaling, then you can just do:

```julia
using Interpolations
xshort = exp.(0:9)
nk = length(xshort)
itp = interpolate(xshort, BSpline(Linear()))
xlong = itp(1:0.5:nk) 

```

---

<div class="post-metadata">

**Author:** ![pxshen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pxshen/32/38776_2.png) [@pxshen](https://discourse.julialang.org/u/pxshen)\
**Post date:** [November 13, 2022, 4:22am UTC](https://discourse.julialang.org/t/tricky-interpolation-issue/90142/7 "2022-11-13T04:22:23Z")

</div>

You can also use `imresize` from `Images`
