store variables in a for loop

4 次查看(过去 30 天)
Hello,
i m trying to obtain a set of coordinates for this:
function [x, y] = gencircle(xo,yo,r,N)
x=(xo+r*cos(theta))';
y=(yo+r*sin(theta))';
clear all
xo=[1 2 3 4 5 6 7 8];
yo=[-1 -0.5];
r=0.01;
N=10;
theta=2*pi/N*(1:N+1);
th=0.00235;
for j=1:length(yo)
for i=1:length(xo)
[xout(:,i), yout(:,j)]=gencircle(xo(i),yo(j),r,N)
[xin(:,i), yin(:,j)]=gencircle(xo(i),yo(j),r-th,N)
end
end
for j=1:length(yo)
for i=1:length(xo)
aa=[xout(:,i),yout(:,j)]
end
end
how can i store the vector aa for each loop?
Thanks

采纳的回答

Sriram Tadavarty
Sriram Tadavarty 2020-4-21
编辑:Sriram Tadavarty 2020-4-21
Hi Davide,
You can make the following modification to the code as shown below, which stores all the loop iteration values in the variable aa:
clear all
xo=[1 2 3 4 5 6 7 8];
yo=[-1 -0.5];
r=0.01;
N=10;
theta=2*pi/N*(1:N+1);
th=0.00235;
for j=1:length(yo)
for i=1:length(xo)
[xout(:,i), yout(:,j)]=gencircle(xo(i),yo(j),r,theta);
[xin(:,i), yin(:,j)]=gencircle(xo(i),yo(j),r-th,theta);
end
end
aa = zeros(size(xout,1),2,length(yo)*length(xo)); % Initialize the variable
tmp = 1;
for j=1:length(yo)
for i=1:length(xo)
aa(:,:,tmp)=[xout(:,i),yout(:,j)];
tmp = tmp+1;
end
end
% aa will be a variable of size length(xout) x 2 x (loop iterations)
% loop iterations here is equal to the product of length(yo) and length(xo) (implies 16)
% Each iteration value can be accessed by indexing from 3rd dimension,
% implies for example fifth iteration can be accessed with aa(:,:,5)
function [x, y] = gencircle(xo,yo,r,theta)
x=(xo+r*cos(theta))';
y=(yo+r*sin(theta))';
end
Hope this helps.
Regards,
Sriram
  4 个评论
Davide Cerra
Davide Cerra 2020-4-21
incredibly useful, thanks!
Sriram Tadavarty
Sriram Tadavarty 2020-4-21
Please do accept the answer, if helped

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by