1

I have a list with 2 cells, each of them is 3 dimension array, one is 3 by 4 by 5, the other is 6 by 7 by 8. Need to transpose the subset in the list to be like: 5 by 4 by 3, and 8 by 7 by 6, I used array.transpose([2,1,0]), got the error axes don't match.

martineau
  • 119,623
  • 25
  • 170
  • 301
xman
  • 183
  • 1
  • 12

1 Answers1

2

Try using the numpy.ndarray.transpose() function instead. Seems like that's closer to what you're looking for.

Documentation: http://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.transpose.html

jackiezhu
  • 422
  • 3
  • 8
  • Use ndarray, I got the error: only length-1 arrays can be converted to Python scalars. – xman Jun 29 '16 at 21:47
  • @xman Convert the list to an array with `np.asarray(input_list)` and then try `swapaxes` or `transpose`. – Divakar Jun 29 '16 at 21:49
  • @Divakar I got another issue now, the h5py file I read from "*.mat" with 2 cells (each of them 3 dimension, 3*4*5 and 6*7*8) now only the 2nd and 3rd axes left after np.array(), and dimension seems to be like (2*4*5) and (2*7*8). – xman Jun 29 '16 at 22:03
  • @xman Ah my bad! I got your question wrongly it seems. So, I think you can process each cell separately. So, if the input list is named `input_list`, you can use `swapaxes` or `transpose` on `input_list[0]` and so on. – Divakar Jun 29 '16 at 22:06
  • The way I read .mat is here: [link](http://stackoverflow.com/questions/27670149/read-matlab-v7-3-file-into-python-list-of-numpy-arrays-via-h5py) – xman Jun 29 '16 at 22:07
  • @Divakar I tried, not work for me.... my problem now is one of the axes missing... – xman Jun 29 '16 at 22:09
  • @Divakar Yes, that's what I was doing. After cell[0].transpose([2,1,0]), it shows that cell[0].shape is still (3, 4, 5), but I want it to be (5, 4, 3)... – xman Jun 29 '16 at 22:23
  • @Divakar Thank you, I'm using pdb debugging, just want to get the array with correct dimension. – xman Jun 29 '16 at 22:29
  • @Divakar Thank you so much!!! I forgot to save it back! Now it works! :) – xman Jun 29 '16 at 22:35