Reputation: 659
I have an array with 112 lines and 40 columns.
The format I need to convert to is 40 sets of 56 points each with x, y.
So, the first line has the x coordinates of the first point in each set. The second line has the x of the second points in the set... until the 56th line. After that I have the y's.
1st line : 40 x's
2nd line: 40 x's
...
56th line: 40 x's
57th line: 40 y's
...
112th line: 40 y's
Initially I thought about doing data.reshape(40, 56, 2)
but that doesn't work because the values for x come before the values for y. If instead I had one line with x's and another with y's that would work though.
Edit:
for i in xrange(len(data)/2):
points.append(data[i])
points.append(data[i+len(data)/2])
points = np.array(points).T.reshape(len(data[0]), len(data)/2, 2)
return points
Upvotes: 2
Views: 1113
Reputation: 70028
I'll use a smaller array (8 x 5) so we can view the returned values easily.
import numpy as NP
# just create a smaller array to work with:
A = NP.random.randint(0, 10, 40).reshape(8, 5)
# split A in half, to separate x and y
p, q = NP.vsplit(A, 2)
# create a 'template' array of the correct dimension
xy = NP.zeros(2, 4, 5)
# now just map the x and y values onto the template
xy[0:,:] = p
xy[1:,:] = q
# the transformed matrix:
array([[[ 8., 5., 2., 5., 7.],
[ 2., 6., 0., 7., 2.],
[ 4., 4., 7., 5., 5.],
[ 8., 5., 2., 0., 5.]],
[[ 4., 8., 6., 9., 2.],
[ 2., 6., 5., 8., 1.],
[ 3., 2., 6., 2., 2.],
[ 1., 8., 0., 7., 3.]]])
Upvotes: 2
Reputation: 61044
Just one idea:
[[(data[i,j], data[i+56,j]) for i in range(56)] for j in range(40)]
Returns a list of list of tuples.
EDIT: Your edit clarifies what you want. If you want pure Numpy, then does this work?
data.reshape(2, 56, 40).swapaxes(0,2)
Upvotes: 4