Filter löschen
Filter löschen

Initializing vs growing a structure

541 Ansichten (letzte 30 Tage)
Steve D
Steve D am 18 Jun. 2012
Kommentiert: Dominique am 13 Sep. 2023
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).

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 2 Jul. 2012
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 Kommentare
Steve D
Steve D am 3 Jul. 2012
Still absorbing all this, but it does sound like the answer I was looking for. Thanks. I'll try it soon.
Steve D
Steve D am 4 Jul. 2012
Bearbeitet: Walter Roberson am 4 Jul. 2012
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.
*** How many bytes are allocated in this case?
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 .
How does the system know when I enter structurename(n) = struct() how many bytes to allocate to cover all the fields that I might add?

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Richard Brown
Richard Brown am 4 Jul. 2012
Bearbeitet: Richard Brown am 4 Jul. 2012
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.

Kategorien

Mehr zu Structures finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by