2012-10-08 102 views
19

Tôi đang dịch một chương trình từ MATLAB sang Python."permute" của MATLAB trong python

Mã matlab sử dụng phương pháp hoán vị:

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. 

Có một phương pháp tương đương bằng Python/NumPy?

+0

[numpy.rollaxis] (http://docs.scipy.org/doc/numpy/reference/generated/numpy.rollaxis.html#numpy.rollaxis) gần ... – mgilson

+2

@Maurits Đó là hoán vị ngẫu nhiên. Vì không có gì ngẫu nhiên trong mô tả về hàm "permute" trong MATLAB, tôi giả sử nó không phải là ngẫu nhiên ... – LynnH

+0

@ user1729698 Lỗi của tôi. – Maurits

Trả lời

22

Điều này được cuộn vào hàm transpose trong numpy.ndarray. Hành vi mặc định đảo ngược thứ tự, nhưng bạn có thể cung cấp một danh sách thứ tự của riêng bạn.

+0

Cảm ơn bạn, đây thực sự là giải pháp :) – LynnH

Các vấn đề liên quan