22
votes

I'm translating a program from matlab to Python.

The matlab code uses the method permute:

B = PERMUTE(A,ORDER) rearranges the dimensions of A so that they
%   are in the order specified by the vector ORDER.  The array produced
%   has the same values as A but the order of the subscripts needed to 
%   access any particular element are rearranged as specified by ORDER.
%   For an N-D array A, numel(ORDER)>=ndims(A). All the elements of 
%   ORDER must be unique.

Is there an equivalent method in Python/NumPy ?

1
numpy.rollaxis is close ... - mgilson
@Maurits That's random permutation. Since there's nothing about random in the description of the function "permute" in matlab i assume it isn't random... - LynnH
@user1729698 My mistake. - Maurits

1 Answers

28
votes

This is rolled into the transpose function in numpy.ndarray. The default behavior reverses the order, but you can supply a list of your own order.