Hi, this is my code for permutation cycles counting. Any idea how to vectorize it to get some additional speedup for large set of permutations?
function count = PermCyclesCount(p)[nR,nE] = size(p);count = zeros(nR,1);for j = 1:nR i = 0; count(j) = 0; while i < nE i = i + 1; if p(j,i) > 0 flag = false; % flag is just to trick the computer into entering the while loop
first = i; while (first ~= i) || ~flag flag = true; r = first; first = p(j,first); p(j,r) = 0; % The entries in the cycle are changed to zero to indicate that they have been `used'
end count(j) = count(j) + 1; end endendend
Some testing data:
[~,perms] = sort(rand(1000000,60),2);tic;c= PermCyclesCount(perms);tocElapsed time is 1.559758 seconds.
Best Answer