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

Chris Colbert sccolbert@gmail....
Fri Jul 10 09:44:03 CDT 2009

```Oh cool, I couldn't figure out with mgrid.
here's what ended up with using broadcasting:

>>> import numpy as np
>>> X = np.zeros((10))
>>> Y = np.arange(10, 20)
>>> M = 10
>>> increments = np.arange(1, M+1)
>>> delta = Y - X
>>> dl = (delta / M).reshape(-1, 1)
>>> interps = dl * increments
>>> lines = X + interps
>>> lines
array([[  1. ,   2. ,   3. ,   4. ,   5. ,   6. ,   7. ,   8. ,   9. ,  10. ],
[  1.1,   2.2,   3.3,   4.4,   5.5,   6.6,   7.7,   8.8,   9.9,  11. ],
[  1.2,   2.4,   3.6,   4.8,   6. ,   7.2,   8.4,   9.6,  10.8,  12. ],
[  1.3,   2.6,   3.9,   5.2,   6.5,   7.8,   9.1,  10.4,  11.7,  13. ],
[  1.4,   2.8,   4.2,   5.6,   7. ,   8.4,   9.8,  11.2,  12.6,  14. ],
[  1.5,   3. ,   4.5,   6. ,   7.5,   9. ,  10.5,  12. ,  13.5,  15. ],
[  1.6,   3.2,   4.8,   6.4,   8. ,   9.6,  11.2,  12.8,  14.4,  16. ],
[  1.7,   3.4,   5.1,   6.8,   8.5,  10.2,  11.9,  13.6,  15.3,  17. ],
[  1.8,   3.6,   5.4,   7.2,   9. ,  10.8,  12.6,  14.4,  16.2,  18. ],
[  1.9,   3.8,   5.7,   7.6,   9.5,  11.4,  13.3,  15.2,  17.1,  19. ]])

On Fri, Jul 10, 2009 at 4:49 AM, David Warde-Farley<dwf@cs.toronto.edu> wrote:
>
> 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
>
> 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
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
```