How to grow a vector in a loop?

조회 수: 5 (최근 30일)
Saurabh Tyagi
Saurabh Tyagi 2021년 10월 15일
댓글: Saurabh Tyagi 2021년 10월 15일
So I have two nested loops
for ii = 1:ldiv+1
for jj = 1:sdiv+1
x_m = sb_panel(ii,jj).xm;
y_m = sb_panel(ii,jj).ym;
z_m = sb_panel(ii,jj).zm;
for kk = 1:ldiv+1
for ll = 1:sdiv+1
x_a = sb_panel(kk,ll).xa;
y_a = sb_panel(kk,ll).ya;
z_a = sb_panel(kk,ll).za;
x_b = sb_panel(kk,ll).xb;
y_b = sb_panel(kk,ll).yb;
z_b = sb_panel(kk,ll).zb;
aa = 1/(((x_m-x_a)*(y_m-y_b)-(x_m-x_b)*(y_m-y_a)));
bb = ((x_b-x_a)*(x_m-x_a)+(y_b-y_a)*(y_m-y_a))/sqrt((x_m-x_a)^2+(y_m-y_a)^2);
cc = ((x_b-x_a)*(x_m-x_b)+(y_b-y_a)*(y_m-y_b))/sqrt((x_m-x_b)^2+(y_m-y_b)^2);
dd = (1/(y_a-y_m))*(1+(x_m-x_a)/sqrt((x_m-x_a)^2+(y_m-y_a)^2));
ee = (1/(y_b-y_m))*(1+(x_m-x_b)/sqrt((x_m-x_b)^2+(y_m-y_b)^2));
coeff = [(aa*(bb-cc)+dd-ee)];
end
end
end
end
The problem is the innermost loop runs as many times as it's supposed to and assigns the last value to 'coeff' but what I want is that each time it runs, it assigns a value to coeff, and then assign the value in next cell when it runs again.

채택된 답변

Matt J
Matt J 2021년 10월 15일
편집: Matt J 2021년 10월 15일
You could just vectorize everything.
x_m = [sb_panel.xm];
y_m = [sb_panel.ym];
z_m = [sb_panel.zm];
x_a = [sb_panel.xa].';
y_a = [sb_panel.ya].';
z_a = [sb_panel.za].';
x_b = [sb_panel.xb].';
y_b = [sb_panel.yb].';
z_b = [sb_panel.zb].';
aa = 1./(((x_m-x_a).*(y_m-y_b)-(x_m-x_b).*(y_m-y_a)));
bb = ((x_b-x_a).*(x_m-x_a)+(y_b-y_a).*(y_m-y_a))./sqrt((x_m-x_a).^2+(y_m-y_a).^2);
cc = ((x_b-x_a).*(x_m-x_b)+(y_b-y_a).*(y_m-y_b))./sqrt((x_m-x_b).^2+(y_m-y_b).^2);
dd = (1./(y_a-y_m)).*(1+(x_m-x_a)./sqrt((x_m-x_a).^2+(y_m-y_a).^2));
ee = (1./(y_b-y_m)).*(1+(x_m-x_b)./sqrt((x_m-x_b).^2+(y_m-y_b).^2));
coeff = reshape( aa.*(bb-cc)+dd-ee , [],1);
  댓글 수: 2
Saurabh Tyagi
Saurabh Tyagi 2021년 10월 15일
Yikes, this worked PERFECTLY. Thank you so much for you time. Can you point out my mistakes and more insight into what you did?
Matt J
Matt J 2021년 10월 15일
Can you point out my mistakes
In your original approach? My other answer describes how to get that to work.
and more insight into what you did?
we first collected the fields of your stucture into vectors
s(1).a=10; s(2).a=20; s(3).a=30;
v=[s.a]
v = 1×3
10 20 30
Then, you just use implicit expansion for all the arithmetic operations, e.g.,
v+(1:4).'
ans = 4×3
11 21 31 12 22 32 13 23 33 14 24 34

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

추가 답변 (2개)

Scott MacKenzie
Scott MacKenzie 2021년 10월 15일
One approach is to declare coeff as an empty array before the first for-statement:
coeff = [];
then add new values to the end of the coeff array as follows:
coeff = [coeff, (aa*(bb-cc)+dd-ee)];
  댓글 수: 2
Saurabh Tyagi
Saurabh Tyagi 2021년 10월 15일
I tried doing that, didn't work. I do get correct answers, but in reverse order. Like if it should be [A B C D], I get [D C B A]
Scott MacKenzie
Scott MacKenzie 2021년 10월 15일
In that case, just reverse the order in the assignment:
coeff = [(aa*(bb-cc)+dd-ee), coeff];

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


Matt J
Matt J 2021년 10월 15일
편집: Matt J 2021년 10월 15일
coef=nan((ldiv+1)^2*(sdiv+1)^2,1); %PRE-ALLOCATE
mm=0;
for ii = 1:ldiv+1
for jj = 1:sdiv+1
...
for kk = 1:ldiv+1
for ll = 1:sdiv+1
....
mm=mm+1;
coeff(mm) = [(aa*(bb-cc)+dd-ee)];
end
end
end
end
  댓글 수: 1
Saurabh Tyagi
Saurabh Tyagi 2021년 10월 15일
Oh that "mm" thing was such an elegant fix to this problem. I have been breaking my head over it for more than a day.

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

카테고리

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

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by