A Simple but useful Python tip.

Want to convert a numpy arrays containing coords from

x1,y1,z1
x2,y2,z2
.
.
.
xn,yn,zn

to

x1
y1
z1
x2
y2
z2
….
xn
yn
zn

Example


print coords
[[ 0.    0.    0.  ]
 [ 0.25  0.    0.  ]
 [ 0.5   0.    0.  ]
 [ 0.75  0.    0.  ]
 [ 1.    0.    0.  ]
 [ 0.    0.25  0.  ]
 [ 0.25  0.25  0.  ]
 [ 0.5   0.25  0.  ]
 [ 0.75  0.25  0.  ]
 [ 1.    0.25  0.  ]
 [ 0.    0.5   0.  ]
 [ 0.25  0.5   0.  ]
 [ 0.5   0.5   0.  ]
 [ 0.75  0.5   0.  ]
 [ 1.    0.5   0.  ]
 [ 0.    0.75  0.  ]
 [ 0.25  0.75  0.  ]
 [ 0.5   0.75  0.  ]
 [ 0.75  0.75  0.  ]
 [ 1.    0.75  0.  ]
 [ 0.    1.    0.  ]
 [ 0.25  1.    0.  ]
 [ 0.5   1.    0.  ]
 [ 0.75  1.    0.  ]
 [ 1.    1.    0.  ]]


change = coords.reshape((-1,1))

print change
[[ 0.  ]
 [ 0.  ]
 [ 0.  ]
 [ 0.25]
 [ 0.  ]
 [ 0.  ]
 [ 0.5 ]
 [ 0.  ]
 [ 0.  ]
 [ 0.75]
 [ 0.  ]
 [ 0.  ]
 [ 1.  ]
 [ 0.  ]
 [ 0.  ]
 [ 0.  ]
 [ 0.25]
 [ 0.  ]
 [ 0.25]
 [ 0.25]
 [ 0.  ]
 [ 0.5 ]
 [ 0.25]
 [ 0.  ]
 [ 0.75]
 [ 0.25]
 [ 0.  ]
 [ 1.  ]
 [ 0.25]
 [ 0.  ]
 [ 0.  ]
 [ 0.5 ]
 [ 0.  ]
 [ 0.25]
 [ 0.5 ]
 [ 0.  ]
 [ 0.5 ]
 [ 0.5 ]
 [ 0.  ]
 [ 0.75]
 [ 0.5 ]
 [ 0.  ]
 [ 1.  ]
 [ 0.5 ]
 [ 0.  ]
 [ 0.  ]
 [ 0.75]
 [ 0.  ]
 [ 0.25]
 [ 0.75]
 [ 0.  ]
 [ 0.5 ]
 [ 0.75]
 [ 0.  ]
 [ 0.75]
 [ 0.75]
 [ 0.  ]
 [ 1.  ]
 [ 0.75]
 [ 0.  ]
 [ 0.  ]
 [ 1.  ]
 [ 0.  ]
 [ 0.25]
 [ 1.  ]
 [ 0.  ]
 [ 0.5 ]
 [ 1.  ]
 [ 0.  ]
 [ 0.75]
 [ 1.  ]
 [ 0.  ]
 [ 1.  ]
 [ 1.  ]
 [ 0.  ]]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s