aboutsummaryrefslogtreecommitdiffstats
path: root/buch/chapters/50-permutationen/beispiele/perm.m
blob: 2e837ef3afb9e1a4b9c54dbe6a7c018d42190195 (plain)
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
#
# 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))