cycles(p) returns a cell array containing the cycle structure of p
0001 function c = cycles(p) 0002 % cycles(p) returns a cell array containing the cycle structure of p 0003 0004 c = {}; 0005 0006 n = length(p); 0007 0008 if n == 0 0009 return 0010 end 0011 0012 used = zeros(1,n); 0013 count = 0; 0014 while (sum(used)<n) 0015 count = count + 1; 0016 avail = find(used == 0); 0017 i = avail(1); 0018 current = i; 0019 used(i) = 1; 0020 while(true) 0021 j = p.array(i); 0022 if used(j) 0023 break 0024 else 0025 current = [current,j]; 0026 i = j; 0027 used(i)=1; 0028 end 0029 end 0030 c{count} = current; 0031 end