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
0 件のコメント
採用された回答
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)
2014 年 2 月 6 日
Questions
- Do you need them all 1.2652e14 at the same time? (Impossible!)
- Do you need only a random fraction of these? (Easy!)
- Do you need specifically the K-th ones? (Do-able)
Answers
- See Roger's answer
- 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 件のコメント
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
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
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!