Search code examples
pythonarraysnumpyanaconda3

Python Numpy Linspace function for bidimensional array


I know it is possible to create numpy arrays using the Linspace function. For example, given a range [x,y] I can make a vector of z elements equally distanced in [x,y]

v = np.linspace(x, y, z, retstep=True)

What if one needs more dimensions? Is it possible to use the same function to generate a 3x4 array? I tried by creating simple arrays and then merge them, but I don't think that is an efficient way to do that


Solution

  • You can use arrays for start and stop point of linspace:

    x=np.linspace((0,0,0), (3,5,14), 4, axis=1)
    print(x)
    

    This will give the output:

    [[ 0.          1.          2.          3.        ]
     [ 0.          1.66666667  3.33333333  5.        ]
     [ 0.          4.66666667  9.33333333 14.        ]]