Skip to content
Advertisement

Supplying varying number of input arguments for np.meshgrid

I have a function that uses np.meshgrid to get the matrix form of supplied co-ordinates. I have a parameter dim that determines what dimension I am working with and needs to return an array with dim dimension along axis 1. I have attached an MWE below.

JavaScript

However my expected output is

JavaScript

, which is obtained by replacing return np.array(np.meshgrid([a]*dim)).T.reshape(-1, dim) with return np.array(np.meshgrid(a, a)).T.reshape(-1, dim). Notice that I passed in (a, a) as the parameters to np.meshgrid, because dim=2. If dim=3, the input would be (a, a, a) and so on.

How can I achieve this? If any other function can do this, I am open to that as well. Thanks.

Advertisement

Answer

Look at the difference between these:

JavaScript

In the first one you are passing a list of lists. In the second one you are unpacking (the first * in func) the list so each sub-list is a positional argument.

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement