Out of memory error in combination combnk
이전 댓글 표시
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.
채택된 답변
추가 답변 (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
Mohsen Rajaei
2014년 2월 6일
Roger Stafford
2014년 2월 6일
Obviously the approach you proposed with 'combnk' is not going to work and you should therefore abandon it.
If you are seeking the real roots to your equation, I would suggest that you make use of matlab's 'fzero' function. You can first do a plot of it over a sufficiently broad span of x values so as to include all zero crossings and that will give you a set of very approximate roots which you can then use for the x0 estimates required by 'fzero' to make the roots accurate
Roger Stafford
2014년 2월 6일
How large a value of K did you have in mind for your problem? If you are determined to find the coefficients of your polynomial, it could be done as follows. For example, if K is equal to 4, the coefficient of x^2 could be found as the sum of all six possible products of two b's and the corresponding 2 x 2 determinant of the a's with the rows and columns that the two b's occupied removed:
b1*b2*det([a33,a34;a43,a44]) +
b1*b3*det([a22,a24;a42,a44]) +
.... +
b3*b4*det([a11,a12;a21,a22]) % Six terms altogether
Note however, that for large K this would need to be done using the appropriate for-loops. For example if K were equal to 11, the coefficient of x^5 would be formed from all possible 462 products of 6 of the 11 b's together with the associated reduced 5 x 5 determinant of a's, and this is too much to have to write out by hand. You could use 'combnk' for this purpose, but hopefully not for anything as large as K = 121.
Mohsen Rajaei
2014년 2월 7일
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
카테고리
도움말 센터 및 File Exchange에서 Performance and Memory에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!