# [SciPy-user] What is the equivalent of MATLAB's "interp2(Z, ntimes)" in Scipy's "interp2d"?

josef.pktd@gmai... josef.pktd@gmai...
Tue Feb 17 14:07:07 CST 2009

```On Tue, Feb 17, 2009 at 9:29 AM, Hani Zahiri <H.Zahiri@curtin.edu.au> wrote:
> Hi Folks,
>
> Would you help me to write this line from MATLAB in Python:
>
> interp2(arr,3)
>
> where 'arr' is an array with shape (2,270).
>
> I tried to use "scipy.interpolate.inetrp2d()" but it doesn't work since
> there is no option to do interleaving interpolates between every elements
> same as what MATLAB does using "interp2(Z, ntimes)"
>
> I'll be happy if you help me with this
>
> Cheers,
>
> Hani
>

I'm not completely sure what the matlab function does, but this may be
the same for interleaving interpolation:
(I didn't see a direct solution with scipy.interpolate)

>>> z.T
array([[  1.,  11.],
[  2.,  12.],
[  3.,  13.],
[  4.,  14.],
[  5.,  15.]])
>>> interp2d_interleave_recursive(z,2).T
array([[  1.  ,   3.5 ,   6.  ,   8.5 ,  11.  ],
[  1.25,   3.75,   6.25,   8.75,  11.25],
[  1.5 ,   4.  ,   6.5 ,   9.  ,  11.5 ],
[  1.75,   4.25,   6.75,   9.25,  11.75],
[  2.  ,   4.5 ,   7.  ,   9.5 ,  12.  ],
[  2.25,   4.75,   7.25,   9.75,  12.25],
[  2.5 ,   5.  ,   7.5 ,  10.  ,  12.5 ],
[  2.75,   5.25,   7.75,  10.25,  12.75],
[  3.  ,   5.5 ,   8.  ,  10.5 ,  13.  ],
[  3.25,   5.75,   8.25,  10.75,  13.25],
[  3.5 ,   6.  ,   8.5 ,  11.  ,  13.5 ],
[  3.75,   6.25,   8.75,  11.25,  13.75],
[  4.  ,   6.5 ,   9.  ,  11.5 ,  14.  ],
[  4.25,   6.75,   9.25,  11.75,  14.25],
[  4.5 ,   7.  ,   9.5 ,  12.  ,  14.5 ],
[  4.75,   7.25,   9.75,  12.25,  14.75],
[  5.  ,   7.5 ,  10.  ,  12.5 ,  15.  ]])
>>> interp2d_interleave_recursive(z,1).T
array([[  1. ,   6. ,  11. ],
[  1.5,   6.5,  11.5],
[  2. ,   7. ,  12. ],
[  2.5,   7.5,  12.5],
[  3. ,   8. ,  13. ],
[  3.5,   8.5,  13.5],
[  4. ,   9. ,  14. ],
[  4.5,   9.5,  14.5],
[  5. ,  10. ,  15. ]])

Josef
-------------- next part --------------

import numpy as np

def interp2d_interleave(z,n):
'''performs linear interpolation on a grid

all points are interpolated in one step not recursively

Parameters
----------
z : 2d array (M,N)
n : int
number of points interpolated

Returns
-------
zi : 2d array ((M-1)*n+M, (N-1)*n+N)
original and linear interpolated values

'''
frac = np.atleast_2d(np.arange(0,n+1)/(1.0+n)).T
zi1 = np.kron(z[:,:-1],np.ones(len(frac))) + np.kron(np.diff(z),frac.T)
zi1 = np.hstack((zi1,z[:,-1:]))
zi2 = np.kron(zi1.T[:,:-1],np.ones(len(frac))) + np.kron(np.diff(zi1.T),frac.T)
zi2 = np.hstack((zi2,zi1.T[:,-1:]))
return zi2.T

def interp2d_interleave_recursive(z,n):
'''interpolates by recursively interleaving n times
'''
zi = z.copy()
for ii in range(1,n+1):
zi = interp2d_interleave(zi,1)
return zi

x = np.linspace(1,5,5)
y = np.linspace(11,15,5)
z = np.vstack((x,y))

print z.T
n = 1
print interp2d_interleave(z,n).T
n = 2
print interp2d_interleave(z,n).T
n = 3
zi3a = interp2d_interleave(z,n)
print zi3a

zi3b = interp2d_interleave_recursive(z,2)
print zi3b

# for linear function recursive and one-step interpolation are the same
# I'm not sure about the general case
print np.all(zi3a==zi3b)
```