Efficient way of preallocating memory
8 views (last 30 days)
Show older comments
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 Comments
Answers (2)
Sean de Wolski
on 24 Feb 2012
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 Comments
Walter Roberson
on 24 Feb 2012
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 Comments
See Also
Categories
Find more on Logical 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!