简体   繁体   中英

matlab nchoosek limitation workaround

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM