How to grow a vector in a loop?

4 次查看(过去 30 天)
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