I would think it would go like this, though you didn't attach your data so I can't test it. Look over the code and logic and see if you agree. It's much simpler than what you tried.
tic; % Generate the same random number for the same seed number.
rand('seed',20001);
% Number of data points
N=40;
% Areal fraction
phi=0.6;
% Dimension of the box
L=14/15; % Length of the box
H=1;% Height of the box
% Diameter of particles
d=sqrt(4*phi*L*H/(pi*N));
% Radius of particles
a=d/2;
for nrun=1:1
nameS=strcat(pdir,'InitCondphi60/','InitCondphi60_',int2str(nrun));
load(nameS);
end
XY0in=[x',y']; % 2 columns matrix
for i= 1:N
% For each particle....
switch ceil(4*rand)
case 1
epsilon=[ -a/4 0];
case 2
epsilon=[+a/4 0];
case 3
epsilon=[0 -a/4];
case 4
epsilon=[0 +a/4];
end % end switch
% Get new tentative particle location
Temposition(i,:) = XY0in(i,:) + epsilon;
% Clip to box. Particle must stay in box.
Temposition(i, 1) = max(Temposition(i,1), -L/2);
Temposition(i, 1) = min(Temposition(i,1), +L/2);
Temposition(i, 2) = max(Temposition(i,1), -H/2);
Temposition(i, 2) = min(Temposition(i,1), +H/2);
% Calculate distances to other particles
otherParticles = XY0in; % Initialize
% Remove row for this particular particle we're looking at now.
otherParticles(i,:) = [];
% Now calculate distance to all other particles.
distances = sqrt((Temposition(i,1) - otherParticles(:,1))^2 + (Temposition(i,2) - otherParticles(:,2))^2);
minDistance = min(distances);
if minDistance > d
% It's good, make the change permanent.
XY0in(i,:) = Temposition(i,:);
% Nothing to do if it's less than d,
% just leave XY0in alone!
end
end