How to insert variable step into numpy mgrid?
I am trying to use numpy.mgrid to create two arrays of arrays, but I want to insert a variable in the number of steps.
Without a variable number of steps, numpy.mgrid works as expected with this code:
x, y = np.mgrid[0:1:3j, 0:2:5j]
But I want something like this because I cannot explicitly specify my step number in the line of code to generate the grid arrays (the values may change due to other processes in my script):
num_x_steps = 3
num_y_steps = 5
x, y = np.mgrid[0:1:(num_x_steps)j, 0:2:(num_y_steps)j] #Try convert to complex
Is there a way to do this that returns a result equivalent to the first method?
I tried running my 3 line code with and without parentheses and tried a couple of other modifications, but nothing worked.
NOTE. I've tried reading this thread, but I'm not sure if this topic applies to this problem; I do not quite understand what it is about, or how he was answered. (Also, I tried to run a line of code from the answer and it returned a MemoryError.) If the topic in question does answer my problem, can someone explain it better and how it applies to my problem?
source to share
The crash is that the j
following parentheses are not converted to a complex number.
In [41]:(1)j
File "<ipython-input-41-874f75f848c4>", line 1
(1)j
^
SyntaxError: invalid syntax
Multiplying the value by 1j
will work, and these lines give the x, y
equivalent to your first line:
num_x_steps = 3 num_y_steps = 5 x, y = np.mgrid[0:1:(num_x_steps * 1j), 0:2:(num_y_steps * 1j)]
source to share