what's the problem in my loop I couldn't understand in this
1 view (last 30 days)
Show older comments
x=linspace(-1,1,256);
[X,Y]=meshgrid(x,x);
r=sqrt(X.^2+Y.^2);
R=0.05;
t = 0.3;
l= 0:36:360;
for i = 1:length(l)
h(i)= t*cos(l(i)); k(i)= t*sin(l(i));
p(i)=sqrt((X-h(i)).^2+(Y-k(i)).^2);
A(i)=(p(i)<R);
end
imagesc(x,x,A(1));colorbar
%this what i want to make.
x=linspace(-1,1,256);
[X,Y]=meshgrid(x,x);
r=sqrt(X.^2+Y.^2);
R=0.05;
t = 0.5;
% i want to apply loop here so it shortnen my code.
h= t*cos(0); k= t*sin(0);
r1=sqrt((X-h).^2+(Y-k).^2);
A1=(r1<R);
h= t*cos(36); k= t*sin(36);
r2=sqrt((X-h).^2+(Y-k).^2);
A2=(r2<R);
h= t*cos(72); k= t*sin(72);
r3=sqrt((X-h).^2+(Y-k).^2);
A3=(r3<R);
h= t*cos(108); k= t*sin(108);
r4=sqrt((X-h).^2+(Y-k).^2);
A4=(r4<R);
h= t*cos(144); k= t*sin(144);
r5=sqrt((X-h).^2+(Y-k).^2);
A5=(r5<R);
h= t*cos(180); k= t*sin(180);
r6=sqrt((X-h).^2+(Y-k).^2);
A6=(r6<R);
h= t*cos(216); k= t*sin(216);
r7=sqrt((X-h).^2+(Y-k).^2);
A7=(r7<R);
h= t*cos(252); k= t*sin(252);
r8=sqrt((X-h).^2+(Y-k).^2);
A8=(r8<R);
h= t*cos(288); k= t*sin(288);
r9=sqrt((X-h).^2+(Y-k).^2);
A9=(r9<R);
h= t*cos(324); k= t*sin(324);
r10=sqrt((X-h).^2+(Y-k).^2);
A10=(r10<R);
h= t*cos(360); k= t*sin(360);
r12=sqrt((X-h).^2+(Y-k).^2);
A12=(r12<R);
A = A1+ A2 + A3 +A4 + A5 + A6 + A7 + A8 + A9+ A10 + A12;
imagesc(x,x,A);colorbar
0 Comments
Accepted Answer
KSSV
on 29 Aug 2022
x=linspace(-1,1,256);
[X,Y]=meshgrid(x,x);
r=sqrt(X.^2+Y.^2);
R=0.05;
t = 0.3;
l= 0:36:360;
A = cell(1,length(l)) ;
for i = 1:length(l)
h= t*cos(l(i));
k= t*sin(l(i));
p=sqrt((X-h).^2+(Y-k).^2);
A{i}=(p<R);
figure
imagesc(A{i});
end
0 Comments
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!