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 :)
asked Feb 25, 2016 at 22:46
Paul Parneix
1492 silver badges9 bronze badges
1 Answer 1
Use the reshape method.
a = np.arange(27) # some 1-D numpy array
a.reshape(-1, 3)
answered Feb 25, 2016 at 22:48
Alex
19.2k9 gold badges65 silver badges82 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Sven Marnach
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.
lang-py