Given a=[1 2 3 4 5], generate m=[2 1 3 4 5; 2 3 1 4 5; 2 3 4 1 5; 2 3 4 5 1; 1 3 2 4 5; 1 3 4 2 5; 1 3 4 5 2; 1 2 4 3 5; 1 2 4 5 3; 1 2 3 5 4].

Given a=[1 2 3 4 5], generate
m=[2 1 3 4 5;
2 3 1 4 5;
2 3 4 1 5;
2 3 4 5 1;
1 3 2 4 5;
1 3 4 2 5;
1 3 4 5 2;
1 2 4 3 5;
1 2 4 5 3;
1 2 3 5 4].
What will be the Matlab code to generate 'm' from 'a'?

2 Comments

Please explain the relation between the input and the output. Why are these 10 permutations chosen, and not the other ones?
There is a relation between i/p and o/p. Observe the first 4 rows of 'm'. In these 4 rows only the first element of 'a', i.e., '1' is shifted to its right one place at a time. Then observe the 5th to 7th row of 'm' where the second element of 'a', i.e., '2' is shifted to its right one place at a time and so on up to the second last element.

Sign in to comment.

 Accepted Answer

Please find the answer of the question asked by me.
Given a=[1 2 3 4 5], generate m=[2 1 3 4 5; 2 3 1 4 5; 2 3 4 1 5; 2 3 4 5 1; 1 3 2 4 5; 1 3 4 2 5; 1 3 4 5 2; 1 2 4 3 5; 1 2 4 5 3; 1 2 3 5 4].
m=[];
k=1;
for i=1:n-1
aa=a;
aa(i)=[];
for j=i+1:n
mm=[];
mm=[aa(1:j-1),a(i),aa(j:n-1)];
m(k,:)=mm;
k=k+1;
end
end

More Answers (0)

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!