You can use perms to swap columns as follows:
% A is given mxn matrix row = 1:size( A, 1 ); col = perms( 1:size( A, 2 ) ); B = zeros( size( col, 1 ), length( row )); % Allocate memory for storage % Simple for-loop (this should be vectorized) % for c = 1:size( B, 2 ) % for r = 1:size( B, 1 ) % B( r, c ) = A( row( c ), col( r, c )); % end % end % Simple for-loop (further vectorization possible) r = 1:size( B, 1 ); for c = 1:size( B, 2 ) B( r, c ) = A( row( c ), col( r, c )); end
source share