[SOLVED] How to transpose first 2 dimensions in 3D array

Issue

How do I transpose the first 2 dimensions of a 3D array ‘matrix?

matrix = np.random.rand(2,3,4)

In the third dimensions I want to swap ‘rows’ with ‘columns’, preferably without a loop.

Solution

You can use the .transpose() function.

matrix = matrix.transpose(1, 0, 2)

means swap the first and the second axis.

Answered By – LukasNeugebauer

Answer Checked By – Clifford M. (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *