Out of memory error in combination combnk

2 次查看(过去 30 天)
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

采纳的回答

Roger Stafford
Roger Stafford 2014-2-6
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

更多回答(2 个)

Jos (10584)
Jos (10584) 2014-2-6
Questions
  1. Do you need them all 1.2652e14 at the same time? (Impossible!)
  2. Do you need only a random fraction of these? (Easy!)
  3. Do you need specifically the K-th ones? (Do-able)
Answers
  1. See Roger's answer
  2. Here is some code:
V = 1:121 ;
K = 10 ;
N = 5 ; % I want five random combinations
[~, x] = sort(rand(N,numel(V)),2) ;
x = sort(x(:,1:K),2) ;
R = V(x) % N random combinations of K unique elements from V
3. upon request
  5 个评论
Mohsen Rajaei
Mohsen Rajaei 2014-2-7
Thank you Roger,
I have tested 'fzero' function, but I had some problems with it. So I tried to use the approach I mentioned above (using combinations). But it seems that it is impossible to use this approach :)
K comes form (2M+1)(2N+1). for M=N=5, K=121. I need 4 or 5 first roots of the equation (and I know that they are less than 1).
My problem with 'fzero' is that, because of large dimensions of the matrix (K=121), the determinant for some x's becomes Inf and 'fzero' returns an error and exits running the code.
My code is something like this:
f=@(x) det(A.*x+B)
for i=linspace(0,1,100)
if abs(f(i))<Inf
r(n)=fzero(f,i)
n=n+1;
end
end
In my equation there is a parameter kx. For some kx's, for all 100 i's (first points to find root), f(i) becomes Inf and the code exits the loop without finding any root!
Roger Stafford
Roger Stafford 2014-2-7
If you are having trouble with your determinant overflowing to infinity, you might try rescaling all the elements in A and B by the same scale factor, s, less than 1. If they all have the same rescaling, that won't affect the roots you obtain for x. Be careful not to make the scale factor too small or you will have trouble underflowing to zero. It could be a delicate adjustment.
Question: What are the significances of M and N in K = (2*M+1)*(2*N+1)? There may be some way of avoiding these huge 121 by 121 matrices which would involve only the sizes M and N. But we can't speculate on this unless you tell us that connection.

请先登录,再进行评论。


Jozef Lukac
Jozef Lukac 2019-1-27
You can generate a specific one combination corresponding to an index idx by this.
N = 6;
k = 4;
pasc = [ones(N,1) zeros(N,k-1)]; %pascal triangle cropped
for i=2:N
for j=2:k
pasc(i,j) = pasc(i-1,j-1) + pasc(i-1,j);
end
end
combs_forCheck = nchoosek(1:N,k)
pasc
%% compute idx-th combination
idx = 7;
auxIdx = idx;
pascK = k; %aux. k index in pascal triang.
pascN = N; %aux N index in pascal triang.
currDig = 1; %current digit
possOut = zeros(1,k); %idx-th combination
while(pascK > 0)
while(pascK>0 && pascN>0 && auxIdx <= pasc(pascN,pascK))
%move up-left the pascal-trinagle matrix
possOut(k-pascK+1) = currDig;
currDig = currDig + 1;
pascN = pascN - 1;
pascK = pascK - 1;
end
if pascK<1
break;
end
while(pascN>0 && auxIdx > pasc(pascN,pascK))
%move up the pascal-trinagle matrix
auxIdx = auxIdx - pasc(pascN,pascK);
currDig = currDig + 1;
pascN = pascN - 1;
end
end
%possOut -- output combination
fprintf(1,'%d, ',possOut); fprintf(1,'\n');
fprintf(1,'%d, ',combs_forCheck(idx,:)); fprintf(1,'\n'); %just check

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by