Home > Enterprise >  Reshaping a coordinate list
Reshaping a coordinate list

Time:10-16

I have 3xN dimensional array containing coordinates ([x1,x2,...xN],[y1,y2,...yN],[z1,z2,...zN]), I need to reshape it into a Nx3 dimensional array of coordinates ([x1,y1,z1],[x2,y2,z2],...,[xN,yN,zN]). I've tried the following:

n=int(1e7)
x=np.linspace(0,1,n)
y=np.linspace(0,1,n)
z=np.linspace(0,1,n)
pos=np.array([x,y,z])

newpos=np.array(list(zip(pos[0],pos[1],pos[2])))

The problem with the code above is that it's to slow for it's purposes. Not only, when using n=1e7 the code runs into a memory error. Is there any other way to achieve the desired purpose?

CodePudding user response:

Test and show this process with a small n:

In [9]: n = 5
In [10]: x=np.linspace(0,1,n)
    ...: y=np.linspace(0,1,n)
    ...: z=np.linspace(0,1,n)
    ...: pos=np.array([x,y,z])
In [11]: pos
Out[11]: 
array([[0.  , 0.25, 0.5 , 0.75, 1.  ],
       [0.  , 0.25, 0.5 , 0.75, 1.  ],
       [0.  , 0.25, 0.5 , 0.75, 1.  ]])
In [12]: newpos=np.array(list(zip(pos[0],pos[1],pos[2])))
In [13]: newpos
Out[13]: 
array([[0.  , 0.  , 0.  ],
       [0.25, 0.25, 0.25],
       [0.5 , 0.5 , 0.5 ],
       [0.75, 0.75, 0.75],
       [1.  , 1.  , 1.  ]])
In [14]: pos.T
Out[14]: 
array([[0.  , 0.  , 0.  ],
       [0.25, 0.25, 0.25],
       [0.5 , 0.5 , 0.5 ],
       [0.75, 0.75, 0.75],
       [1.  , 1.  , 1.  ]])
  • Related