pre-define a struct/array of structs in attempt to get rid of out of memory error.

1 回表示 (過去 30 日間)
Hello, how can i pre-define an array/struct of structs? Motivation. I was dynamically creating an array that grew with every iteration using the sample code below:
ultimate_result = [];
% then in my loop
for i=1:length(items)
items(i).name = item_name; % string
items(i).size = item_size; % double
items(i).results = item_result; % another struct
ultimate_result = [ultimate_result items(i)];
end
now some of you have already predicted, I run out of memory (which is actually the main problem and reason i need this) as the item size grows even with my 32gb or ram. I know before hand the number of items i will need to process.
I have tried something like
ultimate_result(2000) = struct; %
but MATLAB gives the following error.
Unable to perform assignment because the indices on the left side are not compatible with the size of the right side.
Any suggestions to help get rid of the problem will be greatly appreciated. Of course i have tried increasing matlab priority in the task manager in vain,
Am using windows 10, Matlab 2020a, with about 32gb ram.
  2 件のコメント
Stephen23
Stephen23 2021 年 6 月 1 日
"pre-define a struct/array of structs in attempt to get rid of out of memory error"
Preallocating a structure will not reduce its memory consumption.
MatlabEnthusiast
MatlabEnthusiast 2021 年 6 月 4 日
@Stephen Cobeldick indeed the out of memory error persists. What are my options I suspsect that i do indeed run out of memory.

サインインしてコメントする。

採用された回答

Matt J
Matt J 2021 年 6 月 1 日
編集済み: Matt J 2021 年 6 月 1 日
It is not at all clear from what you posted why you might run out of memory, nor why you get the error message you have posted, which has nothing to do with memory limits.
However, assuming "ultimate_ressult" is a typo and should really be "ultimate_result", then you are needlessly updating ultimate_result inside your loop and slowing things down. The whole thing, with proper pre-allocation, can be done as follows.
N=___ % number of "items"
items(N).name=[];
items(N).item_size=[];
items(N).item_result=[];
% then in my loop
for i=1:N
items(i).name = item_name; % string
items(i).size = item_size; % double
items(i).results = item_result; % another struct
end
ultimate_result=items;
  3 件のコメント
MatlabEnthusiast
MatlabEnthusiast 2021 年 6 月 4 日
The pre allocations worked ffine, thanks, but the out of memory error persists. What are my other optiions store this information in files or what?
Matt J
Matt J 2021 年 6 月 4 日
The memory error isn't coming from code that I suggested to you. There are no horzcat operations in the version I posted.

サインインしてコメントする。

その他の回答 (1 件)

Joseph Cheng
Joseph Cheng 2021 年 6 月 1 日
the pre-allocation can be done (though can't really find the right way
ultimateR = [];
temp = struct('name',[],'size',[],'results',[]);
temp(2000).name = 'dummy'; %to be overridden when you actually get to index 2000
for ind = 1:10
temp(ind).name = 'dummy';
temp(ind).size = 10;
temp(ind).results = temp(1);
ultimateR = [ultimateR temp(ind)];
end
I don't think you need your line ultimate_ressult as you're already saving the data in items(i).'parameter'. you're doubling the stored data by re-saving it into ultiate_results which the data is contained in items. i don't get the same error but perhapse something is predefined such that the left and right hand some of one of the above lines isn't right.

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by