diff options
Diffstat (limited to 'buch/chapters/50-permutationen/beispiele')
-rw-r--r-- | buch/chapters/50-permutationen/beispiele/perm.m | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/buch/chapters/50-permutationen/beispiele/perm.m b/buch/chapters/50-permutationen/beispiele/perm.m new file mode 100644 index 0000000..2e837ef --- /dev/null +++ b/buch/chapters/50-permutationen/beispiele/perm.m @@ -0,0 +1,44 @@ +# +# perm.m -- find a random permutation +# +# (c) 2021 Prof Dr Andreas Müller, OST Ostschweizer Fachhochschule +# +N = 8 +M = N+1 + +function retval = permutation(n) + p = (1:n); + for i = (1:(n-1)) + j = i + 1 + floor(rand() * (n-i)); + s = p(i); + p(i) = p(j); + p(j) = s; + endfor + retval = p; +end + +function retval = compose(p,q) + n = size(p)(1,2); + retval = zeros(1,n); + for i = (1:n) + retval(i) = q(p(i)); + end +end + +sigma = permutation(N) +sigma = compose(sigma, permutation(N)) +sigma = compose(sigma, permutation(N)) +sigma = compose(sigma, permutation(N)) +sigma = compose(sigma, permutation(N)) +sigma = compose(sigma, permutation(N)) +sigma = compose(sigma, permutation(N)) + +s = zeros(M,N); +s(1,:) = sigma; +for i = (2:M) + s(i,:) = compose(s(i-1,:), sigma); +end + +s + +compose(sigma, permutation(N)) |