How to stop for loop if E approximately = 0.8.?

1 view (last 30 days)
clc;
clear all;
close all;
[x,y]=meshgrid(-1:2/128:1);
circ=(x.^2+y.^2)<1;
A=ones(129,129);
B=A.*circ;
a1= 12849;
figure(1)
imagesc(B),colormap gray;axis image;axis off;
[x1,y1]=meshgrid(-1:2/128:1);
for r=0:0.001:1
circ1=(x1.^2+y1.^2)<r;
C=B.*circ1;
D=sum(sum(C));
E=D/a1
if (E ~= 0.8)
end
end
figure(2)
imagesc(C),colormap gray;axis image;axis off;

Accepted Answer

John D'Errico
John D'Errico on 19 Jan 2016
tol = 1.e-12;
[x1,y1]=meshgrid(-1:2/128:1);
for r=0:0.001:1
circ1=(x1.^2+y1.^2)<r;
C=B.*circ1;
D=sum(sum(C));
E=D/a1;
if abs(E - 0.8) <= tol
break
end
end
figure(2)
imagesc( C ),colormap gray;axis image;axis off;

More Answers (0)

Categories

Find more on Board games 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!