How to write cyclic permutation as an array in matlab?
13 次查看(过去 30 天)
显示 更早的评论
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.
Thanks in advance
0 个评论
采纳的回答
Jan
2021-7-26
编辑:Jan
2021-7-26
P = [1 4 6 2 5 7 8 3];
Q = zeros(size(P));
Q(circshift(P, -1)) = P
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
isequal(Q, wanted)
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];
3 个评论
Jan
2021-7-27
编辑:Jan
2021-7-27
I do not know, how the input is stored for "(1 4 6 2 5 7 8 3)(9 10)". Therefore I cannot write some code to convert this unkown input to a specific output.
Bold guessing is no fair strategy in programming. But let me try it:
PList = {[1, 4, 6, 2, 5, 7, 8, 3], [9, 10]};
maxIndex = max(cellfun(@max, PList, 'UniformOutput', true));
Q = zeros(1, maxIndex);
for iP = 1:numel(PList)
P = PList{iP};
Q(circshift(P, -1)) = P;
end
Q
So simple apply the code I've suggested in a loop.
It took some time to cope the the error for P==56 in your second example. Did I oversee a point or has this been y typo?
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spline Postprocessing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!