I was wondering if anyone would know how to translate a function I have from Matlab to Python and C++. The main problem I have is to find an interpolation function equivalent to Matlab’s scatteredInterpolant. I have searched for more information and found this other entry but I have not been able to adapt the code, so I was wondering if anyone would know.

The **simplified** **Matlab** function would be as follows

```
function data_out = test_interp (data_input)
U = rand(20,20);
V = rand(20,20);
[X,Y] = meshgrid(1:20,1:20);
% Create matrix with specific format.
aux_array = [reshape(U,[],1) reshape(V,[],1) reshape(X,[],1) reshape(Y,[],1)];
% Apply scatteredInterpolant with vector arrays.
FX = scatteredInterpolant(aux_array(:,1),aux_array(:,2), aux_array(:,3));
FY = scatteredInterpolant(aux_array(:,1),aux_array(:,2), aux_array(:,4));
% Apply FX and FY to the desired input.
data_out(:,1) = FX(data_input(:,1),data_input(:,2));
data_out(:,2) = FY(data_input(:,1),data_input(:,2));
end
```

What it would do would be to perform the interpolation between two matrices and apply them to some input data, a way to call the function would be: `data_out = test_interp (rand(10,2))`

*I note the fact that the matrices are actually much larger and are not randomly generated, but they are useful to understand the effect.*

I was testing and in **Python** what I managed to do is the following:

```
import numpy as np
from scipy.interpolate import griddata
def test_interp (data_input):
# Generate
U = np.random.random([20, 20])
V = np.random.random([20, 20])
[X, Y] = np.meshgrid(np.linspace(1, 20, 20), np.linspace(1, 20, 20))
xx = griddata((X.flatten(), Y.flatten()),U.flatten(),(data_input[:,0],data_input[:,1]),method='linear')
yy = griddata((X.flatten(), Y.flatten()),V.flatten(),(data_input[:,0],data_input[:,1]),method='linear')
data_mm = [xx, yy]
return data_mm
```

But from what I was seeing the griddata function is not well implemented, I also tried with np.interp2d but I didn’t get the same thing.

With **C++** the same thing happens to me, I wouldn’t know which function I should use to replace the Matlab function.

Source: Windows Questions C++