How do I correct this code to not use the 'break' command?
이전 댓글 표시
function P = cities(N,border,hub)
SB = size(border);
SH = size(hub);
if length(N) ~= 1
error('N cannot be a matrix')
elseif N <= 0
error('N must be larger than 0')
elseif mod(N,1) ~= 0
error('N must be an integer')
elseif SH(2) ~= 2 || SH(1) ~= 1
error('Input "border" must be a 1x2 matrix')
elseif SB(2) ~= 2
error('Input "border" must be a two column array')
else
xb = border(:,1)'; %Column 1 of border
yb = border(:,2)'; %Column 2 of border
for i= 1 : N
while (1)
xrand=min(xb)+(max(xb)-min(xb)).*rand(1);%In general, you can generate N random numbers in the interval (a,b) with the formula r = a + (b-a).*rand(N,1).
yrand=min(yb)+(max(yb)-min(yb)).*rand(1);%Used min and max to make the boundary guess a little better
if inpolygon(xrand,yrand,xb,yb)==1; %Exit While loop when inside polygon
break
end
end
Prand(i,:)=[xrand yrand]; %Populate random locations matrix
end
P=[hub;Prand]; %Combine input hub with random locations for output
end
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!