필터 지우기
필터 지우기

Efficient way of preallocating memory

조회 수: 1 (최근 30일)
Med_Imager
Med_Imager 2012년 2월 24일
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!

답변 (2개)

Sean de Wolski
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.

Walter Roberson
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')

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by