I would like to use matlab to create a matrix full of all possible combinations of size 8 in the dataset V= 0:46. This does not seem to be possible using nchoosek. Could someone help me with a workaround? Thank you
Asked
Active
Viewed 427 times
1 Answers
2
If you are willing to create a matrix of 314457495-by-8 elements you can create your own function. A recursive solution would be
function R = nck(v, k)
if k==1,
R = v(:);
elseif k==numel(v),
R = v(:)';
else
R0 = nck(v(1:end-1),k);
R1 = nck(v(1:end-1),k-1);
R = [R0; R1, v(end)*ones(size(R1,1),1)];
end
R = sortrows(R);
end

Mohsen Nosratinia
- 9,844
- 1
- 27
- 52