# Equivalent of map\_coordinates in Julia

**URL:** <https://discourse.julialang.org/t/equivalent-of-map-coordinates-in-julia/5648>\
**Category:** General Usage\
**Created:** [August 31, 2017, 1:50am UTC](https://discourse.julialang.org/t/equivalent-of-map-coordinates-in-julia/5648 "2017-08-31T01:50:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [August 31, 2017, 1:50am UTC](https://discourse.julialang.org/t/equivalent-of-map-coordinates-in-julia/5648/1 "2017-08-31T01:50:41Z")

</div>

Is someone aware of a Julia equivalent of the Python function [`scipy.ndimage.map_coordinates`](https://docs.scipy.org/doc/scipy-0.19.1/reference/generated/scipy.ndimage.map_coordinates.html#scipy.ndimage.map_coordinates)? I’ve looked at [`Interpolations.jl`](https://github.com/JuliaMath/Interpolations.jl) but couldn’t find this exact feature.

In order to port a code from Python to Julia I need this feature, I’d like to avoid duplicating work if it has been already implemented. Or maybe someone is willing to contribute it 🙂

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [September 1, 2017, 1:12am UTC](https://discourse.julialang.org/t/equivalent-of-map-coordinates-in-julia/5648/2 "2017-09-01T01:12:27Z")

</div>

You get it by composition of other functionality (because the task you’re trying to solve is “just” interpolation). Create your input array as an array of `SVector` (from `StaticArrays`) and then use Interpolations.

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [October 2, 2017, 9:35am UTC](https://discourse.julialang.org/t/equivalent-of-map-coordinates-in-julia/5648/3 "2017-10-02T09:35:03Z")

</div>

Thanks for the reply, however I’m not sure I got what you mean. Could you please show how to translate the example at [scipy.ndimage.map\_coordinates — SciPy v0.19.1 Reference Guide](https://docs.scipy.org/doc/scipy-0.19.1/reference/generated/scipy.ndimage.map_coordinates.html)? Here it is for reference:

```python
>>> import numpy as np
>>> from scipy import ndimage
>>> a = np.arange(12.).reshape((4, 3))
>>> a
array([[0., 1., 2.],
       [3., 4., 5.],
       [6., 7., 8.],
       [9., 10., 11.]])
>>> ndimage.map_coordinates(a, [[0.5, 2], [0.5, 1]], order=1)
array([2., 7.])

```

I think my problem is understanding what exactly the Python code does 🙂

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [October 2, 2017, 6:10pm UTC](https://discourse.julialang.org/t/equivalent-of-map-coordinates-in-julia/5648/4 "2017-10-02T18:10:51Z")

</div>

Ok, I managed at least to reproduce the example with this code:

```julia
using Interpolations

function map_coordinates(input, coordinates::AbstractArray{<:AbstractArray{T}, N}) where {T, N}
    output = Array{T,N}(size(coordinates))
    for (i, coord) in enumerate(coordinates)
        output[i] = interpolate(input, BSpline(Linear()), OnCell())[coord[1] + 1, coord[2] + 1]
    end
    return output
end

```

Example (note that the second argument is slightly different from the Python version):

```julia
julia> map_coordinates(reshape(0:11, 3, 4)', [[0.5, 0.5], [2, 1]])
2-element Array{Float64,1}:
 2.0
 7.0

```

Of course this doesn’t completely match all features of `map_coordinates` but can be a starting point.
