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.]]
To do the first one with numpy:
>>> a = np.arange(11)
>>> a[:,None]+a
array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
[ 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
[ 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13],
[ 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14],
[ 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
[ 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16],
[ 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17],
[ 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18],
[ 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]])
For the second array, @Divakar has a good approach. Maybe a bit simpler syntax to do this:
>>> (a%a[:,None])==0
array([[ True, True, True, True, True, True, True, True, True, True, True],
[ True, True, True, True, True, True, True, True, True, True, True],
[ True, False, True, False, True, False, True, False, True, False, True],
[ True, False, False, True, False, False, True, False, False, True, False],
[ True, False, False, False, True, False, False, False, True, False, False],
[ True, False, False, False, False, True, False, False, False, False, True],
[ True, False, False, False, False, False, True, False, False, False, False],
[ True, False, False, False, False, False, False, True, False, False, False],
[ True, False, False, False, False, False, False, False, True, False, False],
[ True, False, False, False, False, False, False, False, False, True, False],
[ True, False, False, False, False, False, False, False, False, False, True]], dtype=bool)
Per your first question:
np.add(*np.indices((nrow, ncol)))
For nrow=5, ncol=6 you get
array([[0, 1, 2, 3, 4, 5],
[1, 2, 3, 4, 5, 6],
[2, 3, 4, 5, 6, 7],
[3, 4, 5, 6, 7, 8],
[4, 5, 6, 7, 8, 9]])
This method doesn't use the numpy.arange function, though I find it more readable. Moreover, it supports cases when nrow != ncol.
You can use numpy.mgrid or numpy.meshgrid():
np.mgrid[0:6, 0:6]
# array([[[0, 0, 0, 0, 0, 0],
# [1, 1, 1, 1, 1, 1],
# [2, 2, 2, 2, 2, 2],
# [3, 3, 3, 3, 3, 3],
# [4, 4, 4, 4, 4, 4],
# [5, 5, 5, 5, 5, 5]],
#
# [[0, 1, 2, 3, 4, 5],
# [0, 1, 2, 3, 4, 5],
# [0, 1, 2, 3, 4, 5],
# [0, 1, 2, 3, 4, 5],
# [0, 1, 2, 3, 4, 5],
# [0, 1, 2, 3, 4, 5]]])
np.meshgrid(np.arange(6), np.arange(6))
# [array([[0, 1, 2, 3, 4, 5],
# [0, 1, 2, 3, 4, 5],
# [0, 1, 2, 3, 4, 5],
# [0, 1, 2, 3, 4, 5],
# [0, 1, 2, 3, 4, 5],
# [0, 1, 2, 3, 4, 5]]),
# array([[0, 0, 0, 0, 0, 0],
# [1, 1, 1, 1, 1, 1],
# [2, 2, 2, 2, 2, 2],
# [3, 3, 3, 3, 3, 3],
# [4, 4, 4, 4, 4, 4],
# [5, 5, 5, 5, 5, 5]])]
and simply unpack the result
a, b = np.mgrid[0:6, 0:6]
You just need numpy.reshape and numpy.repeat. Use this:
import numpy as np
n_columns = 6
a = np.repeat(np.arange(6), n_columns)
a = a.reshape(6,n_columns)
array([[0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 1],
[2, 2, 2, 2, 2, 2],
[3, 3, 3, 3, 3, 3],
[4, 4, 4, 4, 4, 4],
[5, 5, 5, 5, 5, 5]])
b = a.T
array([[0, 1, 2, 3, 4, 5],
[0, 1, 2, 3, 4, 5],
[0, 1, 2, 3, 4, 5],
[0, 1, 2, 3, 4, 5],
[0, 1, 2, 3, 4, 5],
[0, 1, 2, 3, 4, 5]])
This code will work for any n_columns value.