Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Fastest way to reshape numpy 1D array to 4D in a specific sequence?

I want to reshape the flattened channels array of audio to a 4D array(because audio has 4 channels). Reshape example is below:

Input example: [a1,b1,c1,d1,a2,b2,c2,d2,…]

Output 4D array: [[a1,a2,…], [b1,b2,…], [c1,c2,…], [d1,d2,…]]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Each subarray of the 4D array must be one of the channels of audio.
How can I do it in the fastest way?

>Solution :

>>> data = np.arange(20)
>>> data
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
       17, 18, 19])

>>> data.reshape((4, -1), order='F')
array([[ 0,  4,  8, 12, 16],
       [ 1,  5,  9, 13, 17],
       [ 2,  6, 10, 14, 18],
       [ 3,  7, 11, 15, 19]])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading