10

I want to create an array which represent X-Y panel (-50, 50). That is: [[-50, -50], [-49,-50],[-48,-50]....[50,50]], which is at length 101*101.

Clearly, I can generate through a double loop from (-50,50). I'm wondering the prefered way of doing this?

6 Answers 6

10

numpy.meshgrid is obviously the clearest way to me (as @benbo has mentioned), you need one more step to ravel or flatten the 2D grid array:

In [131]: import numpy as np
     ...: x=np.linspace(-2, 2, 5)
     ...: y=np.linspace(-2, 2, 5)
     ...: xx,yy=np.meshgrid(x,y)
     ...: coords=np.array((xx.ravel(), yy.ravel())).T

In [132]: coords
Out[132]: 
array([[-2., -2.],
       [-1., -2.],
       [ 0., -2.],
       [ 1., -2.],
       [ 2., -2.],
       [-2., -1.],
       ......
       [ 1.,  2.],
       [ 2.,  2.]])

In [133]:

Or as @John mentioned, shorten your code with np.c_ to skip the transpose:

coords=np.c_[xx.ravel(), yy.ravel()]

To benchmark:

In [156]: %timeit coords=np.array((xx.ravel(), yy.ravel())).T
100000 loops, best of 3: 14.6 µs per loop

In [157]: %timeit coords=np.c_[xx.ravel(), yy.ravel()] #not as efficient as ↑
10000 loops, best of 3: 47.6 µs per loop
Sign up to request clarification or add additional context in comments.

2 Comments

This is the right way to do it IMO. You can also use np.c_[xx.ravel(), yy.ravel()] and skip the transpose.
interesting -- I wouldn't have expected the np.array way to be faster. Always check with %timeit!
2

How about this:

In [15]: import numpy as np

In [16]: a = np.arange(-3,4)

In [17]: a1 = np.tile(a, (7,1))

In [18]: np.dstack((a1, a1.T)).reshape(-1, 2)

Result:

array([[-3, -3],
       [-2, -3],
       [-1, -3],
       [ 0, -3],
       [ 1, -3],
        ....
       [-1,  3],
       [ 0,  3],
       [ 1,  3],
       [ 2,  3],
       [ 3,  3]])

Comments

1

I'm not 100% sure what you want your output to look like. Does this work for you?

import numpy as np
a=np.linspace(-2, 2, 5)
b=np.linspace(-2, 2, 5)
c,d=np.meshgrid(a,b)
c+d
>>> array([[-4., -3., -2., -1.,  0.],
   [-3., -2., -1.,  0.,  1.],
   [-2., -1.,  0.,  1.,  2.],
   [-1.,  0.,  1.,  2.,  3.],
   [ 0.,  1.,  2.,  3.,  4.]])

1 Comment

Sorry, I misunderstood when you said grid-like
1
>>> import numpy as np
>>> np.array( zip(range(-50,51), [-50] * 50 + [50] * 51) )
array([[-50, -50],
       [-49, -50],
       [-48, -50],
       .
       .
       .
       [ 48,  50],
       [ 49,  50],
       [ 50,  50]])

Comments

0

it maybe looks like this:

In [1]: coords = np.array([[_v, _v] for _v in range(-50, 51)])

In [2]: coords
Out[22]: 
array([[-50, -50],
       [-49, -49],
       [-48, -48],
       ...
       ...
       ...
       [ 48,  48],
       [ 49,  49],
       [ 50,  50]])

Comments

0

Lots of answers! And here's another, based on numpy.indices:

In [1458]: low = -50

In [1459]: high = 51

In [1460]: ndim = 2

In [1461]: coords = (np.indices((high-low,)*ndim) + low)[::-1].reshape(ndim, -1).T

In [1462]: coords
Out[1462]: 
array([[-50, -50],
       [-49, -50],
       [-48, -50],
       ..., 
       [ 48,  50],
       [ 49,  50],
       [ 50,  50]])

If it is not essential that the first coordinate varies the fastest, the reordering achieved by [::-1] can be removed:

In [1463]: coords = (np.indices((high-low,)*ndim) + low).reshape(ndim, -1).T

In [1464]: coords
Out[1464]: 
array([[-50, -50],
       [-50, -49],
       [-50, -48],
       ..., 
       [ 50,  48],
       [ 50,  49],
       [ 50,  50]])

The use of ndim provides a gratuitous feature; it allows for generating a similar array in higher dimensions:

In [1465]: ndim = 3

In [1466]: coords = (np.indices((high-low,)*ndim) + low)[::-1].reshape(ndim, -1).T

In [1467]: coords
Out[1467]: 
array([[-50, -50, -50],
       [-49, -50, -50],
       [-48, -50, -50],
       ..., 
       [ 48,  50,  50],
       [ 49,  50,  50],
       [ 50,  50,  50]])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.