2

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

2 Answers 2

4

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)
Sign up to request clarification or add additional context in comments.

4 Comments

yeah that's what i was looking for. thanks. could you explain to me why you used 0 and 2 though?
Trial and error. :-) reshape confuses me, especially since Matlab traverses column-wise by default, while Numpy traverses row-wise by default. As a sanity check, create numpy.arange(112*40).reshape(112,40). Then reshape it, and check the elements to see if they match the desired output.
i got it. it has 3 dimensions, so when you swap you swap 0 and 2 you swap 2 and 40, meaning you get an array 40 x 56 x 2
Yes, that part I understand. But if you did data.reshape(40, 2, 56).swapaxes(1,2), that would be wrong because the data is not reordered correctly, regardless of the fact that the dimensions are correct.
2

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.]]])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.