Efficient way of preallocating memory
1 回表示 (過去 30 日間)
古いコメントを表示
Hello,
Just wondering is there a better way to preallocate memory for a bunch of 4D arrays. Because so far the way I am doing it, the code stops with the error- Out of memory.
The way I am doing it so far is below
Human_Data.Dimension_Vertical =Size_Human_tmp22(1); % = 256
Human_Data.Dimension_Horizontal=Size_Human_tmp22(2); % = 256
Human_Data.Dimension_Slice =Size_Human_tmp22(4); % = 16
Human_Data.Dimension_Repitition=Size_Human_tmp22(3)/2; % = 64
Human_Data.Image_Matrix_Tag=single(zeros(Human_Data.Dimension_Vertical,Human_Data.Dimension_Horizontal, Human_Data.Dimension_Repitition, Human_Data.Dimension_Slice));
Human_Data.Image_Matrix_Control=single(zeros(Human_Data.Dimension_Vertical,Human_Data.Dimension_Horizontal,Human_Data.Dimension_Repitition, Human_Data.Dimension_Slice));
Human_Data.Image_Matrix_Control_Tag_Sub=single(zeros(Human_Data.Dimension_Vertical,Human_Data.Dimension_Horizontal,Human_Data.Dimension_Repitition,Human_Data.Dimension_Slice));
Human_Data.Image_Matrix_Control_Tag_Avg=single(zeros(Human_Data.Dimension_Vertical,Human_Data.Dimension_Horizontal, Human_Data.Dimension_Repitition, Human_Data.Dimension_Slice));
so all the matrices look like 256 x 256 x 64 x 16
Thanks in advance!
0 件のコメント
回答 (2 件)
Sean de Wolski
2012 年 2 月 24 日
One thing is to use the class input to zeros so that the zero matrix is never created in double precision. As you have it right now:
single(zeros(10));
the matrix is first created in double precision and converted to single later.
zeros(10,10,'single');
avoids this.
0 件のコメント
Walter Roberson
2012 年 2 月 24 日
Instead of single(zeros(P,Q,R,W)), use zeros(P,Q,R,W,'single')
Otherwise you are allocating double-precision values and then throwing away the half memory not needed for 'single'. When you use 'single' as an argument to zeros() then single precision will be allocated directly.
Note: each of your arrays is 256 megabytes when stored as single precision, so you need at least 1 gigabyte of free storage to hold all four arrays.
(By the way, you might want to change 'Repititon' to 'Repetition')
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!