Out of memory error while using for loop

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

回答(2 个)

size() given one argument and one output location always returns a vector. You are comparing a vector in your if test.
Jan
Jan 2018-10-2
编辑:Jan 2018-10-2
"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.

类别

在 帮助中心 和 File Exchange 中查找有关 Multidimensional Arrays 的更多信息

编辑:

Jan
2018-10-2

Community Treasure Hunt

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

Start Hunting!

Translated by