필터 지우기
필터 지우기

How to pre-allocate a struct in Matlab

조회 수: 3 (최근 30일)
Hamed Bolandi
Hamed Bolandi 2019년 7월 19일
댓글: Hamed Bolandi 2019년 7월 19일
I want to pre-allocate stuct e1 and struct e3 in the following loop. I would be thankfull if someone can help.
clc;
clear;
close all;
x=[-30;-25;-20;20;25;30;30;25;20;-20;-25;-30];
y=[-25;-25;-25;-25;-25;-25;25;25;25;25;25;25];
i=x(1:end);
j=y(1:end);
M=[i,j];
s.x=[];
s.y=[];
c=repmat(s,12,1);
c(1).x=M(1,1);
c(1).y=M(1,2);
c(2).x=M(2,1);
c(2).y=M(2,2);
c(3).x=M(3,1);
c(3).y=M(3,2);
c(4).x=M(4,1);
c(4).y=M(4,2);
c(5).x=M(5,1);
c(5).y=M(5,2);
c(6).x=M(6,1);
c(6).y=M(6,2);
c(7).x=M(7,1);
c(7).y=M(7,2);
c(8).x=M(8,1);
c(8).y=M(8,2);
c(9).x=M(9,1);
c(9).y=M(9,2);
c(10).x=M(10,1);
c(10).y=M(10,2);
c(11).x=M(11,1);
c(11).y=M(11,2);
c(12).x=M(12,1);
c(12).y=M(12,2);
for i=1:27
j=c(2);
k=c(4);
l=c(7);
o=c(10);
e3(i,:)=[j;k;l];
for i=1:3:25
o=c(10);
e1(i,:)=o;
end
for i=2:3:26
o=c(11);
e1(i,:)=o;
end
for i=3:3:27
o=c(12);
e1(i,:)=o;
end
for i=4:6
k=c(5);
e3(i,:)=[j;k;l];
end
for i=7:9
k=c(6);
e3(i,:)=[j;k;l];
end
for i=10:18
l=c(8);
e3(i,:)=[j;k;l];
end
for i=13:15
k=c(5);
e3(i,:)=[j;k;l];
end
for i=16:18 %#ok<*FXSET>
k=c(6);
d3(i,:)=[j;k;l];
end
for i=19:27
l=c(9);
e3(i,:)=[j;k;l];
for i=22:24
k=c(5);
e3(i,:)=[j;k;l];
end
for i=25:27
k=c(6);
e3(i,:)=[j;k;l];
end
end
e=[e3,e1];
end
  댓글 수: 4
Rik
Rik 2019년 7월 19일
Try to write some comments in your code what it is doing. You are overwriting entries several times without any clear reason. The answer below (zeros(27,3)) will work, but is likely not your problem.
Hamed Bolandi
Hamed Bolandi 2019년 7월 19일
Thanks for your reply. Below are provided some cooments in my code.
% The objective of this code is to create 81 quadrilaterals with different
% gemoetries. Each quadrilateral has four corners (n=4) and each corner
% has 3 nodes(m=3). Each node from one corner will be connected to
% one node in other 3 corners to creaete a unique quadrilateral. Finally,
% there would be m^n(3^4=81) quadrilaterals.
clc;
clear;
close all;
x=[-30;-25;-20;20;25;30;30;25;20;-20;-25;-30]; % x cordinates of quadrilaterals
y=[-25;-25;-25;-25;-25;-25;25;25;25;25;25;25]; % y cordinates of quadrilaterals
i=x(1:end);
j=y(1:end);
M=[i,j]; % Make a matrix including all x and y coordinates
s.x=[];
s.y=[]; % Make a empty struct
c=repmat(s,12,1); % create an empty 2 fields struct
c(1).x=M(1,1); % Assigning x and y values to the struct. Nodes are labeled from 1 to 12.
c(1).y=M(1,2);
c(2).x=M(2,1);
c(2).y=M(2,2);
c(3).x=M(3,1);
c(3).y=M(3,2);
c(4).x=M(4,1);
c(4).y=M(4,2);
c(5).x=M(5,1);
c(5).y=M(5,2);
c(6).x=M(6,1);
c(6).y=M(6,2);
c(7).x=M(7,1);
c(7).y=M(7,2);
c(8).x=M(8,1);
c(8).y=M(8,2);
c(9).x=M(9,1);
c(9).y=M(9,2);
c(10).x=M(10,1);
c(10).y=M(10,2);
c(11).x=M(11,1);
c(11).y=M(11,2);
c(12).x=M(12,1);
c(12).y=M(12,2);
% Making 3 matrix with dimension of 27X4 in three main loop to make a main matrix with
% dimension of 81x4
% Main loop 1
for i=1:27 % This loop creates the first d=27X4 struct. Breaking struct d into two d3=27x3 and d1=27x1 struct
% and finally combined d1 and d3 to make a sighe 27X4 struct. I break d struct to d3 and d1 since
% d1 and d3 elements are changing in each step. In struct d all the quadrilaterals are starting from
% node number 1(c(1))
j=c(1);
k=c(4);
l=c(7);
o=c(10);
d3(i,:)=[j;k;l];
for i=1:3:25 % Assigning c(10) which x=-20 and y=25 to d1 every 3 row starting from row 1 to row 25
o=c(10);
d1(i,:)=o;
end
for i=2:3:26 % Assigning c(11) which x=-25 and y=25 to d1 every 3 row starting from row 2 to row 26
o=c(11);
d1(i,:)=o;
end
for i=3:3:27 % Assigning c(12) which x=-30 and y=25 to d1 every 3 row starting from row 3 to row 27
o=c(12);
d1(i,:)=o;
end
for i=4:6 % Assigning c(5) which x=25 and y=-25 to the 2nd column of d3 starting from row 4 to row 6
k=c(5);
d3(i,:)=[j;k;l];
end
for i=7:9 % Assigning c(6) which x=30 and y=-25 to the 2nd column of d3 starting from row 7 to row 9
k=c(6);
d3(i,:)=[j;k;l];
end
for i=10:18 % Assigning c(8) which x=25 and y=25 to the 3rd column of d3 starting from row 10 to row 18
l=c(8);
d3(i,:)=[j;k;l];
end
for i=13:15 % Assigning c(5) which x=25 and y=-25 to the 2nd column of d3 starting from row 13 to row 15
k=c(5);
d3(i,:)=[j;k;l];
end
for i=16:18 % Assigning c(6) which x=30 and y=-25 to the 2nd column of d3 starting from row 16 to row 18
k=c(6);
d3(i,:)=[j;k;l];
end
for i=19:27 % Assigning c(9) which x=20 and y=25 to the 2nd column of d3 starting from row 19 to row 27
l=c(9);
d3(i,:)=[j;k;l];
for i=22:24 % Assigning c(5) which x=25 and y=-25 to the 2nd column of d3 starting from row 22 to row 24
k=c(5);
d3(i,:)=[j;k;l];
end
for i=25:27 % Assigning c(6) which x=30 and y=-25 to the 2nd column of d3 starting from row 25 to row 27
k=c(6);
d3(i,:)=[j;k;l];
end
end
d=[d3,d1]; % Combinig d3 and d1 to make a struct d with dimension of 27X4
end
%Repeat all the steps of above loop(main loop 1) for the below loop( main
%loop 2) to make the 2nd 27X4 struct which is named e. In struct e all the
%quadrilaterals are starting from node number 2(c(2))

댓글을 달려면 로그인하십시오.

채택된 답변

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2019년 7월 19일
편집: madhan ravi 2019년 7월 19일
Did you mean memory pre-allocation? if so, then
e1=zeros(27,1);
e3=ones(27,3);

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by