How to pre-allocate a struct in Matlab
조회 수: 3 (최근 30일)
이전 댓글 표시
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
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.
채택된 답변
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
추가 답변 (0개)
참고 항목
카테고리
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!