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
.
Asked
Active
Viewed 96 times
1
-
Use `numpy.swapaxes`. – Mad Physicist Jun 29 '16 at 21:09
-
@MadPhysicist will try, but now I got another issue, seems one axes missing when I read the file from h5py – xman Jun 29 '16 at 21:51
-
Print the shape of your arrays, the commands you are running, the actual, full errors you are getting. Your comments don't match what you are saying in the question. – Mad Physicist Jun 30 '16 at 14:40
1 Answers
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
-