out of memory error using ndgrid
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
a=1:500;
b=1:4;
c=0:2;
d=0:1;
e=0:1;
f=1:15;
I tried running a code, which is
[a, b, c, d, e, f]=ndgrid(sets{:});
but it shows that
Error using repmat
Out of memory. Type HELP MEMORY for your options.
How can I solve this problem? Can I make the code simpler so that it does not take that much memory?
채택된 답변
Walter Roberson
2016년 9월 26일
We do not know. What are the values in sets ? What do you do with the data afterwards?
If sets = {a, b, c, d, e, f} according to the initial assignments of a, b, c, d, e, f that you show, then the total memory for the output of ndgrid is not much, less than 18 megabytes.
However, if you were working in a loop like
a=1:500;
b=1:4;
c=0:2;
d=0:1;
e=0:1;
f=1:15;
for K = 1 : 5
sets = {a, b, c, d, e, f};
[a, b, c, d, e, f] = ndgrid(sets{:});
...
end
then after you do the ndgrid once, because you are overwriting a, b, c, d, e, f, on the second iteration of the loop, the variables would have 360000 elements each, and you would run out of memory trying to ndgrid that .
Re-using variable names for different purposes is poor programming practice that leads to this kind of problem.
댓글 수: 7
SANG GYU LEE
2016년 9월 26일
편집: Walter Roberson
2016년 9월 26일
I am trying to make a code which shows every combination of input matrix. In this case, the output will be a 6 column (from a to f) matrix.
All arrays from a to f contain possible numbers that I might be selected. For the case I gave as an example, the output might be [1 1 0 0 0 1; 1 1 0 0 0 2;1 1 0 0 0 3;...;1 1 0 0 0 15; 1 1 0 0 1 1; ...; 500 4 2 1 1 15].
Walter Roberson
2016년 9월 26일
편집: Walter Roberson
2016년 9월 27일
nset = length(sets);
[T1{1:nset] = ndgrid(sets{:});
output = zeros(numel(T1{1}), nset, class(T1{1})); %corrected
for K = 1 : nset
output(:,K) = reshape(T1{K}, [], 1);
end
If you still run out of memory doing the ndgrid then you probably do not have enough memory to complete the operation.
However, if you are running an older version of MATLAB, where repmat was not a built-in operation, then the way repmat was written was not always the best if you were short on memory. In that particular case you would be better off writing the functionality yourself in terms of loops -- or, better yet, writing the functionality as mex. In current versions, repmat is built in and more efficient. (This is relevant because ndgrid relies on repmat.)
SANG GYU LEE
2016년 9월 27일
편집: Walter Roberson
2016년 9월 27일
Thank you very much for your help.
I made a following function
function output=testcomb(a, b, c, d, e, f)
sets = {a, b, c, d, e, f};
nset = length(sets);
[T1{1:nset}] = ndgrid(sets{:});
output = zeros(numel(T1{1}, nset, class(T1{1})));
for K = 1 : nset
output(:,K)=reshape(T1{K},[], 1);
end
I get an error
Error in testcomb (line 7)
output(:,K)=reshape(T1{K},[], 1);
Could you please help me fix this?
function output=testcomb(a, b, c, d, e, f)
sets = {a, b, c, d, e, f};
nset = length(sets);
[T1{1:nset}] = ndgrid(sets{:});
output = zeros(numel(T1{1}), nset, class(T1{1})); %corrected
for K = 1 : nset
output(:,K)=reshape(T1{K},[], 1);
end
SANG GYU LEE
2016년 9월 28일
Well, I am sorry. I still have the same error on the line of
output(:,K)=reshape(T1{K},[], 1);
What values are you passing in? What is the exact error message, everything in red?
I tested the code here with the values you showed earlier,
a=1:500;
b=1:4;
c=0:2;
d=0:1;
e=0:1;
f=1:15;
and it seems to work fine after the correction to the assignment to output that I noted above.
If you are using MS Windows, please also post the output of
memory
SANG GYU LEE
2016년 9월 29일
I closed Matlab and tried your code again. It works well. Thank you very much for your help. I really appreciate.
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
제품
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
