2

Sorry for this simple question, but I can't find how to figure it out :

I have a long 1D numpy array like:

[1,2,3,4,5,6,7,8,9,10,11,12, ... ,n1,n2,n3]

this array is used to store x y z position of points, like [x0,y0,z0,x1,y1,z1 etc.... ]

I would like to convert it to this form :

[ [1,2,3],[4,5,6],[7,8,9],[10,11,12],....,[n1,n2,n3] ]

It it possible with numpy without going through slow for loops ?

Thanks :)

1 Answer 1

3

Use the reshape method.

a = np.arange(27)  # some 1-D numpy array
a.reshape(-1, 3)
Sign up to request clarification or add additional context in comments.

1 Comment

Note that this doesn't loop over the data at all, i.e. not even in the C library code. It only creates a new view of the same data with different array strides pointing to the same region in memory as the original array.

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.