constructor for the permutation class
p = permutation(n) --- create a new (identity) permutation on 1:n
p = permutation(vec) --- create a new permutation based on the array vec
(which should be a permutation of 1:n).
CROSS-REFERENCE INFORMATION
This function calls:
length length(p) gives the size of the permutation
size size(p) returns the number of elements permuted and the number of cycles.
random random(p) --- return a random reordering of the elements of p
SOURCE CODE
0001 function p = permutation(n)
0002 % constructor for the permutation class
0003 % p = permutation(n) --- create a new (identity) permutation on 1:n
0004 % p = permutation(vec) --- create a new permutation based on the array vec
0005 % (which should be a permutation of 1:n).
0006
0007 if nargin==0
0008 n = 1;
0009 end
0010
0011 bad = 'Input invalid: must be a permutation of 1:n or a single integer';
0012 [r,c] = size(n);
0013
0014 if (r>1) && (c>1)
0015 error(bad);
0016 end
0017
0018 if (r==1) && (c==1)
0019 x = 1:n;
0020 else
0021 x = n;
0022 end
0023
0024
0025 x = x(:)';
0026 n = length(x);
0027
0028
0029 y = sort(x);
0030 if any(y ~= 1:n)
0031 error(bad);
0032 end
0033
0034 p.array = x;
0035 p = class(p,'permutation');