Initializing vs growing a structure

486 vues (au cours des 30 derniers jours)
Steve D
Steve D le 18 Juin 2012
Commenté : Dominique le 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).

Réponse acceptée

Walter Roberson
Walter Roberson le 2 Juil 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 commentaires
Steve D
Steve D le 3 Juil 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 le 4 Juil 2012
Modifié(e) : Walter Roberson le 4 Juil 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?

Connectez-vous pour commenter.

Plus de réponses (1)

Richard Brown
Richard Brown le 4 Juil 2012
Modifié(e) : Richard Brown le 4 Juil 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.
  1 commentaire
Dominique
Dominique le 13 Sep 2023
Great solution.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by