Out of memory error in combination combnk
조회 수: 3 (최근 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
댓글 수: 2
Alejandro Casares
2025년 1월 5일
Isn't there some way to modify nchoosek or another similar function to make it write the huge matrix to disk?
Then, the user could read it row by row and process each one independently, without overflowing memory.
A. Casares M.
DGM
2025년 1월 6일
If you can assume each combination of N numbers can be somehow packed into a single byte, then you're still going to need over 100 terabytes of disk space. That doesn't seem very practical either.
채택된 답변
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!