Out of memory error while using for loop
Show older comments
I have a 1x46 cell of different length cells.i want to zero pad each cell to maximum length cell because i want to apply cell2mat function and in order apply that i need to make this cell consistent.But whenever i run my code it gives memory error.maximum length is 6691240 i.e there are 6691240 elements in the maximum length cell
for i=1:numel(X1)
if size(X1{i}) < 6691240
X1{i}(end,6691240)=0;
end
end
Answers (2)
Walter Roberson
on 2 Oct 2018
0 votes
size() given one argument and one output location always returns a vector. You are comparing a vector in your if test.
"gives memory error" might mean, that you are running out of memory. If X1{i} is e.g. a 6691240x1 vector, size(X1{i}) replies [6691240, 1] and [6691240, 1] < 6691240] is the vector [false, true]. The |if command requires a scalar condition, such that Matlab inserts am all() automatically. To be exact:
if size(X1{i}) < 6691240
is evaluated as
tmp = size(X1{i}) < 6691240
if all(tmp) && ~isempty(tmp)
If a cell of the X1 array contains a 6691240x1 vector, the code X1{i}(end,6691240)=0 expands it to a 6691240x6691240 array, which requires 358 TB RAM. This will most likely exhaust your machine.
One 6691240x1 vector requires 53.5 MB. If X1 has e.g. 80 elements, it needs 4 GB. So it is possible, that your array consumes more memory that you have installed.
Categories
Find more on Multidimensional Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!