3

I have a very large numpy array of matrix that has the structure:

np.array([
           [[1, 2],
            [3, 4]],
           [[5, 6],
            [7, 8]],
         ])

my expected output is

np.array([
           [[1, 3],
            [2, 4]],
           [[5, 7],
            [6, 8]],
         ])

I know methods using list comprehension or for loops and then create the numpy array again, but methods involving creating the numpy from list is too slow for my data. And vectorize seems to be only working on numbers.

When I use normal python lists or other languages, I can map the transpose function into the list, but it seems there is no similar function in numpy.

How can I do it efficiently?

5
  • Use swapaxes : arr.swapaxes(1,2) Commented Jun 29, 2016 at 10:34
  • 3
    arr.transpose(0, 2, 1)? Commented Jun 29, 2016 at 10:34
  • 1
    yeah just use the transpose() - method docs.scipy.org/doc/numpy/reference/generated/… Commented Jun 29, 2016 at 10:55
  • Thanks for your advices. Commented Jun 29, 2016 at 11:05
  • Check if this answers your question: stackoverflow.com/questions/33990279/… Commented Sep 9, 2020 at 9:17

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.