out of memory error using ndgrid

6 次查看(过去 30 天)
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
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 个评论
Walter Roberson
Walter Roberson 2016-9-28
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
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 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by