how to find different permutations of columns of a matrix such that at any permutation just two columns change place

what permutations are there if we want n-2 columns be the same and just 2 columns swap. Here is an example of what I mean: a=[1 2 3 4;5 6 7 8;9 10 11 12;13 14 15 16] then indices would be (1,2,3,4) now I want to know how can I implement (2,1,3,4) (3,2,1,4) (4,2,3,1) (1,3,2,4) (1,4,3,2) (1,2,4,3) as you see at any permutation just 2 indices are swapping and the rest are the same as in (1,2,3,4) Thank you in advance

Answers (1)

c = nchoosek(1:n,2);
p = repmat(1:n,size(c,1),1);
for k = 1:size(c,1)
p(k,c(k,:)) = p(k,c(k,[2,1]);
end
Each row of 'p' will be one of the desired permutations to be applied to the columns of 'a'. For example, the k-th row of p can be used to compute
bk = a(:,p(k,:));

Categories

Find more on Language Fundamentals in Help Center and File Exchange

Asked:

on 25 Jul 2014

Edited:

on 25 Jul 2014

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!