How to write disjoint cycles in matlab?

5 views (last 30 days)
lilly lord
lilly lord on 2 Aug 2021
Answered: Vidhi Agarwal on 5 Dec 2024 at 4:14
a and b are from S-16 permutation written as disjoint cycle
a=(1 3 5 7 9 11) b=(2 4 6 8 10 12 14) .
%disjoint cycles a=(1 3 5 7 9 11),b=(2 4 6 8 10 12 14 16)
a=[1:16;11 2 1 4 3 6 5 8 7 10 9 12 13 14 15 16];%missing numbers in 'a' are fixed
b=[1:16;1 16 3 2 5 4 7 6 9 8 11 10 13 12 15 14];% missing numbers in 'b' are fixed
It is easy to write disjoint cycles as written above but for large matrices for example for any permutation of 200 elements, writing fixed points (which may not be even or odd permutations)are very difficult . Can we do it in matlab to write fixed points automatically. For example i am talking a permutation
P=[1:200;1,3,15,13] % only four points are moving the rest are fixed. so how can it be done in matlab. Thanks for any help in advance

Answers (1)

Vidhi Agarwal
Vidhi Agarwal on 5 Dec 2024 at 4:14
You can represent permutations using disjoint cycles by creating a function that constructs these cycles and applies them to a set. Disjoint cycles are a way to express permutations where each element moves to another element in a cycle, and elements not mentioned in any cycle are fixed.
You can follow the given below steps to create and use disjoint cycles in MATLAB:
  • Specify the cycles you want to use. Each cycle is a list of elements that are permuted among themselves.
  • Write a function to apply these cycles to a set of numbers, typically from 1 to n.
  • Elements not included in any cycle remain unchanged.
Below is the sample code that might help you in getting started:
function perm = applyDisjointCycles(n, cycles)
% n: Total number of elements in the set (e.g., 16)
% cycles: Cell array where each cell contains a vector representing a cycle
% Initialize the permutation as an identity permutation
perm = 1:n;
% Apply each cycle to the permutation
for i = 1:length(cycles)
cycle = cycles{i};
numElements = length(cycle);
for j = 1:numElements
% Map each element to the next in the cycle, wrapping around at the end
nextIndex = mod(j, numElements) + 1;
perm(cycle(j)) = cycle(nextIndex);
end
end
end
n = 16;
cyclesA = {[1, 3, 5, 7, 9, 11]};
cyclesB = {[2, 4, 6, 8, 10, 12, 14, 16]};
permA = applyDisjointCycles(n, cyclesA);
permB = applyDisjointCycles(n, cyclesB);
disp('Permutation A:');
disp(permA);
disp('Permutation B:');
disp(permB);
Hope this helps!

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!