# How to grow a vector in a loop?

9 views (last 30 days)
Saurabh Tyagi on 15 Oct 2021
Commented: Saurabh Tyagi on 15 Oct 2021
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 on 15 Oct 2021
Edited: Matt J on 15 Oct 2021
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 CommentsShowHide 1 older comment
Matt J on 15 Oct 2021
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

Scott MacKenzie on 15 Oct 2021
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 CommentsShowHide 1 older comment
Scott MacKenzie on 15 Oct 2021
In that case, just reverse the order in the assignment:
coeff = [(aa*(bb-cc)+dd-ee), coeff];

Matt J on 15 Oct 2021
Edited: Matt J on 15 Oct 2021
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
Saurabh Tyagi on 15 Oct 2021
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.

R2021b

### Community Treasure Hunt

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

Start Hunting!