[Numpy-discussion] Re: How do I use numpy to do this?
Robert Kern
robert.kern at gmail.com
Thu Jun 1 12:21:02 CDT 2006
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
More information about the Numpy-discussion
mailing list