MATLAB: Out of memory error in combination combnk

combinationcombnkout of memory error

Hi
I want to get all combinations of the n elements in v taken k at a time. I use combnk(v,k). For example combnk(1:121,10). But it is a huge matrix and MATLAB return an "out of memory" error.
I want to use each of the rows of the c in a loop. Is there any way to get each row of the c (one by one) in the loop to avoid out of memory error.
Thanks a lot
M. Rajaei

Best Answer

The total number of rows you would get from combnk(1:121,10) is 121!/10!/111! which is 1.2652e14 which is a very large number indeed. Even if you produced them in a loop one-at-a-time, the loop would require a very long time to finish up. Are you sure this is what you want to do?
A rather ugly way to generate them one-at-a-time would be in nested for-loops ten deep:
for i1 = 1:121-9
for i2 = i1+1:121-8
for i3 = i2+1:121-7
for i4 = i3+1:121-6
....
for i10 = t9+1:121
% Now process the set of values i1,i2,i3,i4,...,i10 as desired
end
....
end
end
end
end
Related Question