Hi, I am trying to produce randomly circles with same radius in defined range or square. I wrote the code, but it show error " Out of memory. The likely cause is an infinite"

조회 수: 1 (최근 30일)
function h = circle(x,y,r, N)
%INPUT
%x= range of x
%y= range of y
%OUTPUT
% circles between x and y range with same radious
hold on
x=[0 10];
y=[0 10];
r=5;
N=5;
for i=1:1:N
while 1
x1=min(x)+(max(x)-min(x))*rand(1);
y1=min(y)+(max(y)-min(y))*rand(1);
th = 0:pi/50:2*pi;
x2 = r * cos(th) + x1;
y2 = r * sin(th) + y2;
for i=1:1:size(circle)
x1=circle(i,1);
y1=circle(i,2);
x2=circle(i,3);
y2=circle(i,4);
h = plot([x1, y1],[x2, y2], "b");
pos = [min(x1,x2) min(y1,y2) abs(x2-x1) abs(y2-y1)];
rectangle('Position',pos,'Curvature',[1 1]);
end
end
end
hold off

답변 (1개)

Walter Roberson
Walter Roberson 2023년 1월 24일
편집: Walter Roberson 2023년 1월 24일
function h = circle(x,y,r, N)
...
for i=1:1:size(circle)
circle is not a local variable in the function circle, and is not the target of an import statement. Therefore the reference to size(circle) is a request to execute the function circle with no parameters, and when that call eventually returns, to return a vector of the size of the return value of the function circle. The return value of size() is always at least two values. You are using that vector with the : operator. When you use a non-scalar with a : operator, then the effect is the same as taking only the first value in the array.
So that section of code is equivalent to
temporary_variable = circle(); %call circle
temporary_variable2 = size(temporary_variable); %take the size of what circle() returned
for i = 1 : 1 : temporary_variable2(1)
and in turn that would be equivalent to
temporary_variable = circle(); %call circle
for i = 1 : 1 : size(temporary_variable,1)
So what is this function circle that needs to be executed in order to discard its output other than to take the number of rows of the output? Why, circle is the very function that you are executing. So the function circle cannot finish executing to return a value until the function circle finishes executing (which cannot finish until the function circle() finishes executing, which cannot finish until....)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by