Question: Structure Array Generation and Dimensions Manipulation

4 views (last 30 days)
Hello.
I'm encountering a unexpected different in the outcomes while using a method to generate a structure array. Specifically, when running the code, I noticed that the dimensions vary: one outcome displays as "4x1 struct" (where the "4" comes first) and the other as "1x2 struct" (with the "2" at the end). Could someone explain why this difference occurs?
Furthermore, I'm seeking guidance on how to modify the dimensions of the "1x2 struct" to become "2x1 struct." Could you please provide insights or code snippets to accomplish this?
Lastly, I'm interested in merging the "4x1 struct" with the "1x2 struct." Could someone demonstrate how to combine these structures effectively? I have attached the code for reference.
Thank you in advance for your assistance.
clear
clc
% Let's say
empty_individual.Position=[];
empty_individual.Cost=[];
empty_individual.Rank=[];
empty_individual.DominationSet=[];
empty_individual.DominatedCount=[];
pop=repmat(empty_individual,4,1)
pop = 4x1 struct array with fields:
Position Cost Rank DominationSet DominatedCount
for i=1:4
pop(i).Position=rand(1,2);
pop(i).Cost= pop(i).Position.^2;
pop(i).Fitness=1;
end
pop
pop = 4x1 struct array with fields:
Position Cost Rank DominationSet DominatedCount Fitness
% --------------------------------------------------
for j=1:2
newpop(j).Position=rand(1,2);
newpop(j).Cost= newpop(j).Position.^2;
newpop(j).Fitness=3;
newpop(j).Rank=[]
newpop(j).DominationSet=2
newpop(j).DominatedCount=3
end
newpop = struct with fields:
Position: [0.9255 0.9216] Cost: [0.8566 0.8494] Fitness: 3 Rank: []
newpop = struct with fields:
Position: [0.9255 0.9216] Cost: [0.8566 0.8494] Fitness: 3 Rank: [] DominationSet: 2
newpop = struct with fields:
Position: [0.9255 0.9216] Cost: [0.8566 0.8494] Fitness: 3 Rank: [] DominationSet: 2 DominatedCount: 3
newpop = 1x2 struct array with fields:
Position Cost Fitness Rank DominationSet DominatedCount
newpop = 1x2 struct array with fields:
Position Cost Fitness Rank DominationSet DominatedCount
newpop = 1x2 struct array with fields:
Position Cost Fitness Rank DominationSet DominatedCount
newpop
newpop = 1x2 struct array with fields:
Position Cost Fitness Rank DominationSet DominatedCount
% ------------------------------------------------
% How to combine it together? I use this method... it appears error.
[newpop;pop]
Error using vertcat
Dimensions of arrays being concatenated are not consistent.

Accepted Answer

Stephen23
Stephen23 on 27 Mar 2024
Edited: Stephen23 on 27 Mar 2024
Simple solution:
new = [newpop(:);pop(:)]
However if you want to your code to be more robust, then you need to preallocate NEW/POP before the loop:
  • at least as a scalar structure, even if it has no fields, or
  • the entire structure array using e.g. CELL and STRUCT:
C = cell(4,1);
P = struct('Position',C, 'Cost',C, 'Rank',C, 'DominationSet',C, 'DominationCount',C);

More Answers (1)

Tejas
Tejas on 27 Mar 2024
Hello Wei,
It seems you are encountering an error while trying to concatenate two structures of different dimensions. The root of this issue lies in the lack of memory pre-allocation.
  • When the first structure was created, the repmat function allocated a structure with the dimension of 4X1.
  • However, during the creation of the second structure, memory pre-allocation was not performed, and values were directly assigned to the structure.
  • This led MATLAB to assume default dimensions for the structure, resulting in a structure of dimension 1X2.
To resolve this, before assigning values to the second structure, pre-allocate memory for it using the repmat function and define the desired dimension within it as follows:
newpop = repmat(empty_individual, 2, 1);
This approach will create a structure of dimension 2X1, which can then be concatenated with the first structure of dimension 4X1 without any errors.
For more detailed information on the repmat function, please refer to this documentation:
Hope it helps!

Categories

Find more on Structures in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!