[Numpy-discussion] Re: How do I use numpy to do this?

Sasha ndarray at mac.com
Thu Jun 1 12:27:02 EDT 2006


>>> mgrid[0:10, 5:15].reshape(2,100).transpose()
array([[ 0,  5],
       [ 0,  6],
       [ 0,  7],
       [ 0,  8],
       ...])


On 6/1/06, Robert Kern <robert.kern at gmail.com> wrote:
> Christopher Barker wrote:
> >
> > I'm trying to get the (x,y) coords for all the points in a grid, bound
> > by xmin, xmax, ymin, ymax.
> >
> > This list comprehension does it fine:
> >
> > Points = [(x,y) for x in xrange(minx, maxx) for y in xrange(miny, maxy)]
> >
> > But I can't think at the moment how to do it with numpy. Any ideas?
>
> In [4]: x, y = mgrid[0:10, 5:15]
>
> In [5]: x
> Out[5]:
> array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
>        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
>        [2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
>        [3, 3, 3, 3, 3, 3, 3, 3, 3, 3],
>        [4, 4, 4, 4, 4, 4, 4, 4, 4, 4],
>        [5, 5, 5, 5, 5, 5, 5, 5, 5, 5],
>        [6, 6, 6, 6, 6, 6, 6, 6, 6, 6],
>        [7, 7, 7, 7, 7, 7, 7, 7, 7, 7],
>        [8, 8, 8, 8, 8, 8, 8, 8, 8, 8],
>        [9, 9, 9, 9, 9, 9, 9, 9, 9, 9]])
>
> In [6]: y
> Out[6]:
> array([[ 5,  6,  7,  8,  9, 10, 11, 12, 13, 14],
>        [ 5,  6,  7,  8,  9, 10, 11, 12, 13, 14],
>        [ 5,  6,  7,  8,  9, 10, 11, 12, 13, 14],
>        [ 5,  6,  7,  8,  9, 10, 11, 12, 13, 14],
>        [ 5,  6,  7,  8,  9, 10, 11, 12, 13, 14],
>        [ 5,  6,  7,  8,  9, 10, 11, 12, 13, 14],
>        [ 5,  6,  7,  8,  9, 10, 11, 12, 13, 14],
>        [ 5,  6,  7,  8,  9, 10, 11, 12, 13, 14],
>        [ 5,  6,  7,  8,  9, 10, 11, 12, 13, 14],
>        [ 5,  6,  7,  8,  9, 10, 11, 12, 13, 14]])
>
> In [8]: points = column_stack((x.ravel(), y.ravel()))
>
> In [9]: points
> Out[9]:
> array([[ 0,  5],
>        [ 0,  6],
>        [ 0,  7],
>        [ 0,  8],
>        [ 0,  9],
>        [ 0, 10],
> ...
>
> --
> Robert Kern
>
> "I have come to believe that the whole world is an enigma, a harmless enigma
>  that is made terrible by our own mad attempt to interpret it as though it had
>  an underlying truth."
>   -- Umberto Eco
>
>
>
> -------------------------------------------------------
> All the advantages of Linux Managed Hosting--Without the Cost and Risk!
> Fully trained technicians. The highest number of Red Hat certifications in
> the hosting industry. Fanatical Support. Click to learn more
> http://sel.as-us.falkag.net/sel?cmd=lnk&kid=107521&bid=248729&dat=121642
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/numpy-discussion
>




More information about the NumPy-Discussion mailing list