You can use np.mgrid for this, it's often more convenient than np.meshgrid because it creates the arrays in one step:
import numpy as np
X,Y = np.mgrid[-5:5.1:0.5, -5:5.1:0.5]
For linspace-like functionality, replace the step (i.e. 0.5) with a complex number whose magnitude specifies the number of points you want in the series. Using this syntax, the same arrays as above are specified as:
X, Y = np.mgrid[-5:5:21j, -5:5:21j]
You can then create your pairs as:
xy = np.vstack((X.flatten(), Y.flatten())).T
As @ali_m suggested, this can all be done in one line:
xy = np.mgrid[-5:5.1:0.5, -5:5.1:0.5].reshape(2,-1).T
Best of luck!
Answer from farenorth on Stack OverflowYou can use np.mgrid for this, it's often more convenient than np.meshgrid because it creates the arrays in one step:
import numpy as np
X,Y = np.mgrid[-5:5.1:0.5, -5:5.1:0.5]
For linspace-like functionality, replace the step (i.e. 0.5) with a complex number whose magnitude specifies the number of points you want in the series. Using this syntax, the same arrays as above are specified as:
X, Y = np.mgrid[-5:5:21j, -5:5:21j]
You can then create your pairs as:
xy = np.vstack((X.flatten(), Y.flatten())).T
As @ali_m suggested, this can all be done in one line:
xy = np.mgrid[-5:5.1:0.5, -5:5.1:0.5].reshape(2,-1).T
Best of luck!
This is just what you are looking for:
matr = np.linspace((1,2),(10,20),10)
This means:
For the first column; from 1 of (1,2) to 10 of (10,20), put the increasing 10 numbers.
For the second column; from 2 of (1,2) to 20 of (10,20), put the incresing 10 numbers.
And the result will be:
[[ 1. 2.]
[ 2. 4.]
[ 3. 6.]
[ 4. 8.]
[ 5. 10.]
[ 6. 12.]
[ 7. 14.]
[ 8. 16.]
[ 9. 18.]
[10. 20.]]
You may also keep only one column's values increasing, for example, if you say that:
matr = np.linspace((1,2),(1,20),10)
The first column will be from 1 of (1,2) to 1 of (1,20) for 10 times which means that it will stay as 1 and the result will be:
[[ 1. 2.]
[ 1. 4.]
[ 1. 6.]
[ 1. 8.]
[ 1. 10.]
[ 1. 12.]
[ 1. 14.]
[ 1. 16.]
[ 1. 18.]
[ 1. 20.]]
In [4]: xstep = np.linspace(0, 1, 2)
In [5]: ystep = np.linspace(0, 1, 3)
In [6]: xstep[:, None] + 1j*ystep
Out[6]:
array([[0.+0.j , 0.+0.5j, 0.+1.j ],
[1.+0.j , 1.+0.5j, 1.+1.j ]])
xstep[:, None] is equivalent to xstep[:, np.newaxis] and its purpose is to add a new axis to xstep on the right. Thus, xstep[:, None] is a 2D array of shape (2, 1).
In [19]: xstep[:, None].shape
Out[19]: (2, 1)
xstep[:, None] + 1j*ystep is thus the sum of a 2D array of shape (2, 1) and a 1D array of shape (3,).
NumPy broadcasting resolves this apparent shape conflict by automatically adding new axes (of length 1) on the left. So, by NumPy broadcasting rules, 1j*ystep is promoted to an array of shape (1, 3).
(Notice that xstep[:, None] is required to explicitly add new axes on the right, but broadcasting will automatically add axes on the left. This is why 1j*ystep[None, :] was unnecessary though valid.)
Broadcasting further promotes both arrays to the common shape (2, 3) (but in a memory-efficient way, without copying the data). The values along the axes of length 1 are broadcasted repeatedly:
In [15]: X, Y = np.broadcast_arrays(xstep[:, None], 1j*ystep)
In [16]: X
Out[16]:
array([[0., 0., 0.],
[1., 1., 1.]])
In [17]: Y
Out[17]:
array([[0.+0.j , 0.+0.5j, 0.+1.j ],
[0.+0.j , 0.+0.5j, 0.+1.j ]])
You can use np.ogrid with imaginary "step" to obtain linspace semantics:
y, x = np.ogrid[0:1:2j, 0:1:3j]
y + 1j*x
# array([[0.+0.j , 0.+0.5j, 0.+1.j ],
# [1.+0.j , 1.+0.5j, 1.+1.j ]])
Here the ogrid line means make an open 2D grid. axis 0: 0 to 1, 2 steps, axis 1: 0 to 1, 3 steps. The type of the slice "step" acts as a switch, if it is imaginary (in fact anything of complex type) its absolute value is taken and the expression is treated like a linspace. Otherwise range semantics apply.
The return values
y, x
# (array([[0.],
# [1.]]), array([[0. , 0.5, 1. ]]))
are "broadcast ready", so in the example we can simply add them and obtain a full 2D grid.
If we allow ourselves an imaginary "stop" parameter in the second slice (which only works with linspace semantics, so depending on your style you may prefer to avoid it) this can be condensed to one line:
sum(np.ogrid[0:1:2j, 0:1j:3j])
# array([[0.+0.j , 0.+0.5j, 0.+1.j ],
# [1.+0.j , 1.+0.5j, 1.+1.j ]])
A similar but potentially more performant method would be preallocation and then broadcasting:
out = np.empty((y.size, x.size), complex)
out.real[...], out.imag[...] = y, x
out
# array([[0.+0.j , 0.+0.5j, 0.+1.j ],
# [1.+0.j , 1.+0.5j, 1.+1.j ]])
And another one using outer sum:
np.add.outer(np.linspace(0,1,2), np.linspace(0,1j,3))
# array([[0.+0.j , 0.+0.5j, 0.+1.j ],
# [1.+0.j , 1.+0.5j, 1.+1.j ]])