Hi Timo,
It appears that for n even, the number of possible combinations of nonintersecting pairs is the product of all the odd integers less than n. The following code seems to work. The matrix B is one answer, but A = n+1-fliplr(B) puts A into a form like yours; if you run this for n=6 you can see the difference. Past n = 10 you are going to need semicolons!
For n = 18 this takes about 8 seconds on my PC and creates a matrix with 17!! x 18 = 6.2e8 elements. I went to uint8 numbers for the matrices so each of the them has as only as many bytes as elements, 620MBytes.
n = 6
B = paircombs(n)
A = n+1-fliplr(B)
function m = paircombs(n)
if n==2
m = uint8([1 2]);
else
q = paircombs(n-2);
qrow = size(q,1);
m = repmat(q, n-1,1);
onz = ones(1,size(m,1),'uint8')';
m = [m (n-1)*onz n*onz];
for k = 1:n-2
ind = k*qrow+1:(k+1)*qrow;
msub = m(ind,:);
msub(msub==(n-(k+1))) = n-1;
msub(:,end-1) = n-(k+1);
m(ind,:) = msub;
end
end
end
Best Answer