store variables in a for loop

5 ビュー (過去 30 日間)
Davide Cerra
Davide Cerra 2020 年 4 月 21 日
コメント済み: Sriram Tadavarty 2020 年 4 月 21 日
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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by