Numpy: Interleave values of singular 1D array to 2D array
Question:
Given an 1D numpy array that looks liked [1,2,3,4,5,6]
is there a built-in function to produce an interleaved 2D that looks like [[1,3,5],[2,4,6]]
?
Shape of input: (x,)
Desired shape of output: (2, x/2)
Have been looking through the docs for numpy and have yet to come across anything that can do this with a singular array. All functions and other questions dealt with two arrays. I only have a singlular array.
Thanks 🙂
Answers:
reshape
in F
order:
a = np.array([1,2,3,4,5,6])
out = a.reshape(2, -1, order='F')
Output:
array([[1, 3, 5],
[2, 4, 6]])
Given an 1D numpy array that looks liked [1,2,3,4,5,6]
is there a built-in function to produce an interleaved 2D that looks like [[1,3,5],[2,4,6]]
?
Shape of input: (x,)
Desired shape of output: (2, x/2)
Have been looking through the docs for numpy and have yet to come across anything that can do this with a singular array. All functions and other questions dealt with two arrays. I only have a singlular array.
Thanks 🙂
reshape
in F
order:
a = np.array([1,2,3,4,5,6])
out = a.reshape(2, -1, order='F')
Output:
array([[1, 3, 5],
[2, 4, 6]])