# Execution of while loop - iteration times vary hugely , or the loop gets stuck...

4 views (last 30 days)
jj17 on 12 Jan 2015
Edited: dpb on 12 Jan 2015
Hi,
My code generates two types of random points within a circle (which is divided into 3 regions), and calculates distances between them. There are certain constraints on the minimum distance between these two types of random points - u and R, executed using while true . The problem is that it gets stuck on certain iterations where the execution takes quite long time - sometimes few minutes ( or gets stuck?),whereas other iterations are running in a very short time (on average around 0.1s). This means something is wrong with the code, but I cannot figure out what it is...Hence, I would greatly appreciate help on this.
In the first part of the code, I generate R points (10 of them per per a region of circle), and that runs fine. The problem is with the second part, where I generate u points (2 u points per each R point, and they are within a certain distance of R points). There, I compute the distance of each u point with (0,0) and with all other previously generated R points. The code for the second part is given below. distance is just a function calculating the distance between the two points with x and y coordinates.
beta=[pi/6,5*pi/6,3*pi/2,13*pi/6];
for sect_indx=1:no_regions
for own_R_ind=1:own_R_per_region;
for u_indx=1:Nu_clustter
while true
f=rand;
g=rand;
r1=dist_u_in_R*sqrt(f);
theta1 = (beta(sect_indx+1)-beta(sect_indx)) * g + beta(sect_indx);
% generation of u points
x_pos_u(i,u_indx,own_R_ind)= x_pos_R(i,own_R_ind,sect_indx) + ...
r1*cos(theta1);
y_pos_u(i,u_indx,own_R_ind)=y_pos_R(i,own_R_ind,sect_indx) + ...
r1*sin(theta1);
% calculate their distance to (0,0)
u_clustter_2_0{i,u_indx,own_R_ind} =
distance(0,x_pos_u(i,u_indx,own_R_ind), ...
0,y_pos_u(i,u_indx,own_R_ind));
u_clusster_centre=cell2mat( u_clustter_2_0(i,u_indx,own_R_ind));
% calculate their distance to all previously generated R points
for kk=1:total_no_R_per_region*no_regions
u_clustter_2_all_R{i,u_indx,own_R_ind,kk}=...
distance(x_pos_u(i,u_indx,own_R_ind), ...
x_R_abs_indx(i,kk), ...
y_pos_u(i,u_indx,own_R_ind), ...
y_R_abs_indx(i,kk));
end
temp_u_clustter_2_R(i,u_indx,own_R_ind,:)= ...
cell2mat(u_clustter_2_all_R(i,u_indx,own_R_ind,:));
if(temp_u_clustter_2_R(i,u_indx,own_R_ind,:)>=min_u_R_dist)& ...
(u_clusster_centre>=min_u_dist_to_0) & ...
plot(x_pos_u(i,u_indx,own_R_ind), ...
y_pos_u(i,u_indx,own_R_ind),'r*') ;
break
end
end
end
end
end