TransposeMat - like transpose, but handles N-D arrays

by

 

16 May 2013 (Updated )

Transposes the rows/columns (only) on any N-dimensional array

TransposeMat(in)
% TRANSPOSEMAT Matrix transpose
%     TRANSPOSEMAT(mat) returns mat with the rows and columns interchanged.
%     For a 2-D matrix, this is the same as issuing the native Matlab
%     command 'transpose', or using the non-conjuncate transpose operator
%     (i.e. mat = mat.';)  However, for higher dimensional arrays, it simply
%     swaps the row and column dimensions, leaving the other dimensions
%     unchanged
function out = TransposeMat(in)

   N = length(size(in));
   switch N
      case 2 % 2-D
         out = in.';
      otherwise % N-D
         dim = [2 1 3:N];
         out = permute(in,dim);         
   end
end

Contact us