필터 지우기
필터 지우기

store variables in a for loop

조회 수: 4 (최근 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 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