Skip to content Skip to sidebar Skip to footer

Flatten Numpy Array Without Double For Loop

I have a 2-d matrix. For the purposes of this example, let's say it is a random matrix >>> a = np.random.randn(5, 7) >>> a array([[-0.37279322, 0.28619523, -0.05

Solution 1:

Use np.meshgrid to create 2D meshes corresponding to X and Y labels and then stack them as columns alongwith the 2D input array a, like so -

X,Y = np.meshgrid(label_x,label_y)
out = np.column_stack((Y.ravel(),X.ravel(),a.ravel()))

Post a Comment for "Flatten Numpy Array Without Double For Loop"