フィルターのクリア

How to grow a vector in a loop?

6 ビュー (過去 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 Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by