[Numpy-discussion] an np.arange for arrays?

David Warde-Farley dwf@cs.toronto....
Fri Jul 10 03:49:44 CDT 2009


On 10-Jul-09, at 1:25 AM, Chris Colbert wrote:

> actually what would be better is if i can pass two 1d arrays X and Y
> both size Nx1
> and get back a 2d array of size NxM where the [n,:] row is the linear
> interpolation of X[n] to Y[n]

This could be more efficient, but here's a solution using mgrid and  
broadcasting:

def interp_grid_thingie(X, Y, M):
	frac = np.mgrid[0:1:M*1j]
	return (1 - frac[np.newaxis, :]) * X[:, np.newaxis] +  
frac[np.newaxis, :] * Y[:, np.newaxis]

In [11]: interp_grid_thingie(arange(1,6), arange(0,5), 5)
Out[11]:
array([[ 1.  ,  0.75,  0.5 ,  0.25,  0.  ],
        [ 2.  ,  1.75,  1.5 ,  1.25,  1.  ],
        [ 3.  ,  2.75,  2.5 ,  2.25,  2.  ],
        [ 4.  ,  3.75,  3.5 ,  3.25,  3.  ],
        [ 5.  ,  4.75,  4.5 ,  4.25,  4.  ]])

David


More information about the NumPy-Discussion mailing list