Fastest solution so far is my rahter clumsy attempt at using logical indexing to solve the problem:
m = 10;
n = 4;
b = nchoosek(m,n);
ii = true(m,b);
ii(nchoosek(1:m,n)'+(0:m:m*b-1))=false;
xr = repmat([1:m]',1,b);
v = reshape(xr(~ii),n,b)';
w = reshape(xr(ii),m-n,b)';
Readability can be improved upon. But definitely fast.
Note that this method can also take input other than 1:m:
x = [8 8 8 6 1 2 3 4 10 4];
n = 4;
m = size(x,2);
b = nchoosek(m,n);
ii = true(m,b);
ii(nchoosek(1:m,n)'+(0:m:m*b-1))=false;
xr = repmat(x',1,b);
v = reshape(xr(~ii),n,b)';
w = reshape(xr(ii),m-n,b)';
Sidenote: A possibility for nchoosek to output remaining values directly would be quite a nice feature.
1 Comment
Direct link to this comment
https://ww2.mathworks.cn/matlabcentral/answers/276055-how-can-i-use-nchoosek-to-output-both-the-k-combinations-and-the-remaining-combinations#comment_699288
Direct link to this comment
https://ww2.mathworks.cn/matlabcentral/answers/276055-how-can-i-use-nchoosek-to-output-both-the-k-combinations-and-the-remaining-combinations#comment_699288
Sign in to comment.