I have got a question regarding all the combinations of matrix-rows in Matlab. I currently create a combination-matrix out of a two-column-matrix:
Input:
1 2
1 3
1 4
2 3
2 4
3 4
What I get is the following:
1 2 3 4
1 3 2 4
1 4 2 3
And when the input-matrix has entries from 1:6 it looks like this:
1 2 3 4 5 6
1 2 3 5 4 6
1 2 3 6 4 5
1 3 2 4 5 6
1 3 2 5 4 6
....
Right now I've implemented the following solution, which works nearly perfect (Thanks to Luis Mendo):
M = [1 2
1 3
1 4
2 3
2 4
3 4]; %// example data
n = floor(max(M(:))/2); %// size of tuples. Compute this way, or set manually
p = nchoosek(1:size(M,1), n).'; %'// generate all n-tuples of row indices
R = reshape(M(p,:).', n*size(M,2), []).'; %// generate result...
R = R(all(diff(sort(R.'))),:); %'//...removing combinations with repeated values
The problem I have now is the size. I need this matrix for an optimization algorithm, but the nchoosek-command creates a hugh matrix which gets shortend with the last command-line. I can actually use this solution only for an input-vector with the lenght of 15 digits, because the nchoosek-command cannot handle more.
What I am searching now, is a way to create theses combinations without the nchoosek-command. Does somebody has an idea how to?
Thanks and best regard
Jonas