# How to write cyclic permutation as an array in matlab?

5 views (last 30 days)
lilly lord on 26 Jul 2021
Commented: lilly lord on 27 Jul 2021
I have disjoint permutation cycles such as (1 4 6 2 5 7 8 3)(9 10) means
1 comes under 4th position, 4 will be at 6th position ,6 will be at 2nd position and so on. 3 will be at first position .(9,10) means that 9 will ne at 10 position and 10 will be placed at 9th position.(Look at the second row of the output given below)
S=[1:10; 3 6 8 1 2 4 5 7 10 9];
out put is
1 2 3 4 5 6 7 8 9 10
3 6 8 1 2 4 5 7 10 9
It is easy when we have small permutation, but for large set like
perm=[22, 35, 17, 9, 11, 23, 56, 43, 25, 19, 26, 15, 28, 30, 6, 1, 33, 24, 12, 14, 47, 18, 20, 31, 41, 39, 32, 27, 34, 13, 21, 4, 2, 16, 8, 49, 7, 38, 40, 29, 10, 3, 5, 36, 42, 37, 44, 46, 48, 50];
required=[1:50;6 4 10 21 3 30 45 16 17 29 9 24 34 12 26 2 35 47 25 18 13 50 11 33 43 19 32 15 40 28 20 39 1 27 22 5 42 7 41 38 31 36 23 37 49 44 14 46 8 48 ];
output is
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
6 4 10 21 3 30 45 16 17 29 9 24 34 12 26 2 35 47 25 18 13 50 11 33 43 19 32 15 40 28 20 39 1 27 22 5 42 7 41 38 31 36 23 37 49 44 14 46 8 48
Is there a way in matlab that we can fill up the second row for large pemutation it is difficult to write manually.

Jan on 26 Jul 2021
Edited: Jan on 26 Jul 2021
P = [1 4 6 2 5 7 8 3];
Q = zeros(size(P));
Q(circshift(P, -1)) = P
Q = 1×8
3 6 8 1 2 4 5 7
P = [22, 35, 17, 9, 11, 23, 56, 43, 25, 19, 26, 15, 28, 30, 6, 1, ...
... % ^^ Greater than length(P) ?!?
33, 24, 12, 14, 47, 18, 20, 31, 41, 39, 32, 27, 34, 13, 21, 4, ...
2, 16, 8, 49, 7, 38, 40, 29, 10, 3, 5, 36, 42, 37, 44, 46, 48, 50];
wanted = [6 4 10 21 3 30 45 16 17 29 9 24 34 12 26 2 35 47 25 18 13 50, ...
11 33 43 19 32 15 40 28 20 39 1 27 22 5 42 7 41 38 31 36 23 37 49, ...
44 14 46 8 48];
Q = zeros(size(P));
Q(circshift(P, -1)) = P
Q = 1×56
6 4 10 21 3 30 49 16 17 29 9 24 34 12 26 2 35 47 25 18 13 50 11 33 43 19 32 15 40 28
isequal(Q, wanted)
ans = logical
0
There is a typo in your data at P==56. The 45 is missing, but it must be at another location.
This is working to get your output: wanted
P = [22, 35, 17, 9, 11, 23, 43, 25, 19, 26, 15, 28, 30, 6, 1, 33, ...
24, 12, 14, 47, 18, 20, 31, 41, 39, 32, 27, 34, 13, 21, 4, 2, ...
16, 8, 49, 45, 7, 38, 40, 29, 10, 3, 5, 36, 42, 37, 44, 46, 48, 50];
lilly lord on 27 Jul 2021
p=56 is written by mistake. Your code works excellent . Thanks a lot