[Numpy-discussion] How do I use numpy to do this? (original) (raw)

Christopher Barker Chris.Barker at noaa.gov
Thu Jun 1 18:13:28 EDT 2006


Robert Kern wrote:

points = mgrid[minx:maxx, miny:maxy].reshape(2, -1).transpose()

As I need Numeric and numarray compatibility at this point, it seems the best I could come up with is below. I'm guessing the list comprehension may well be faster!

-Chris

#!/usr/bin/env python

#import numpy as N #import Numeric as N import numarray as N

Spacing = 2.0

minx = 0 maxx = 5 miny = 20 maxy = 22

print "minx", minx print "miny", miny print "maxx", maxx print "maxy", maxy

# The nifty, terse, numpy way

points = mgrid[minx:maxx, miny:maxy].reshape(2, -1).transpose()

The Numeric and numarray way:

x = N.arange(minx, maxx+Spacing, Spacing) # makeing sure to get the last point y = N.arange(miny, maxy+Spacing, Spacing) # an extra is OK points = N.zeros((len(y), len(x), 2), N.Float) x.shape = (1,-1) y.shape = (-1,1) points[:,:,0] += x points[:,:,1] += y points.shape = (-1,2)

print points

-- Christopher Barker, Ph.D. Oceanographer

NOAA/OR&R/HAZMAT (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker at noaa.gov



More information about the NumPy-Discussion mailing list