Efficient way of preallocating memory
3 次查看(过去 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!