Initializing vs growing a structure
조회 수: 519 (최근 30일)
이전 댓글 표시
I have read the advice on how to avoid inefficient use of memory by preallocation of matrices using zeros for eg. I am having trouble translating that into how to preallocate memory for a structure which has various fields, including some matrices. The structure has to be created a field at a time. But I want to avoid "growing" it and wasting memory.
Could I create a huge array the size of the final structure, and then just start creating structure fields using that matrix name, and avoid thereby duplicate things on the heap while the structure is being built.
If not what is recommended Details appreciated. (Not using Windows, So memory command not available).
댓글 수: 0
채택된 답변
Walter Roberson
2012년 7월 2일
Like this:
Stucture_Size = 78965; %number of elements
StructureName(Structure_Size) = struct();
Then StructureName will become a structure array of the proper size, starting with no fields.
As you add a previously-unused field to any element in the structure array, the field will be added to every element in the structure array, initialized to the empty double array in those other structure array elements. For example, if you set
StructureName(1).MyNewField = 'A' : 'Q';
then StructureName(483).MyNewField would pop into existence as the empty double array, not as an empty char array.
Each structure array member can have its field be a different type.
StructureName(1).MyNewField = 'A' : 'Q';
StructureName(2).MyNewField = serial('COM3'); %completely different type is OK!
When you make an assignment to all of an ordinary MATLAB variable, or you make an assignment to all of a particular structure array field name, then the memory used for the expression on the right hand side is shared with the left-hand side, not copied (but memory is copied if you assign to part of a variable, such as assigning to Q(8) = 17 ). Because of this, most often you do not need to preallocate the memory for a structure array member field name:
StructureName(278).MyNewField = zeros(1,500); %legal but often useless
In particular the preallocation would be useless if you then assign to all of the member's field, such as
StructureName(278).MyNewField = rand(1,500);
It would not, however, be useless if you immediately start working with just part of the variable, such as
StructureName(278).MyNewField(43:59) = 0.5;
In a numeric array, if you had created the first 277 rows then assigning the 278'th row would be inefficient, but in structure arrays as long as you have pre-allocated the structure array itself, there is no inefficiency for having created values only for StructureName(1:277).MyNewField and not having preallocated for StructureName(278).MyNewField .
댓글 수: 2
추가 답변 (1개)
Richard Brown
2012년 7월 4일
편집: Richard Brown
2012년 7월 4일
Let's say you wanted to create a 100x1 struct array with a field A that you know will store a 3x3 matrix
struct('A', repmat({zeros(3)}, 100, 1))
will do the trick.
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!