用于在图上分配点的 Matlab 代码
Matlab Code to distribute points on plot
我编辑了我在网上找到的一个代码,它可以帮助我根据它们之间的最小距离以某种方式在图表上绘制点分布
这是我目前的代码
x(1)=rand(1)*1000; %Random coordinates of the first point
y(1)=rand(1)*1000;
minAllowableDistance = 30; %IF THIS IS TOO BIG, THE LOOP DOES NOT END
numberOfPoints = 300; % Number of points equivalent to the number of sites
keeperX = x(1); % Initialize first point
keeperY = y(1);
counter = 2;
for k = 2 : numberOfPoints %Dropping another point, and checking if it can be positioned
done=0;
trial_counter=1;
while (done~=1)
x(k)=rand(1)*1000;
y(k)=rand(1)*1000;
thisX = x(k); % Get a trial point.
thisY = y(k);
% See how far is is away from existing keeper points.
distances = sqrt((thisX-keeperX).^2 + (thisY - keeperY).^2);
minDistance = min(distances);
if minDistance >= minAllowableDistance
keeperX(k) = thisX;
keeperY(k) = thisY;
done=1;
trial_counter=trial_counter+1;
counter = counter + 1;
end
if (trial_counter>2)
done=1;
end
end
end
end
所以这段代码工作正常,但有时如果点数超过 600,matlab 会冻结。问题已满,并且不再添加点数,因此 matlab 一遍又一遍地完成工作。所以我需要找到一种方法,当 trial_counter 大于 2 时,找到一个空的 space 并在那里定居。
trial_counter用于第三次不合适的扣分。
谢谢
由于 trial_counter=trial_counter+1;
仅在 if minDistance >= minAllowableDistance
内部调用,如果 minDistance < minAllowableDistance
很容易进入无限循环(例如,如果您现有的点非常紧凑)。
如何执行此操作取决于您的局限性,但如果您正在查看设定范围内的整数点,一种可能性是将这些点保留为二进制图像,并使用 bwdist
来算出距离变换,然后选择一个可接受的点。因此每次迭代都是(其中 BW
是您存储的 "image"/2D 二进制矩阵,其中 1 是所选点):
D = bwdist(BW);
maybe_points = find(D>minAllowableDistance); % list of possible locations
n = randi(length(maybe_points)); % pick one location
BW(maybe_points(n))=1; % add it to your matrix
(然后添加一些检查,如果找不到任何允许的点,则循环退出)
我编辑了我在网上找到的一个代码,它可以帮助我根据它们之间的最小距离以某种方式在图表上绘制点分布
这是我目前的代码
x(1)=rand(1)*1000; %Random coordinates of the first point
y(1)=rand(1)*1000;
minAllowableDistance = 30; %IF THIS IS TOO BIG, THE LOOP DOES NOT END
numberOfPoints = 300; % Number of points equivalent to the number of sites
keeperX = x(1); % Initialize first point
keeperY = y(1);
counter = 2;
for k = 2 : numberOfPoints %Dropping another point, and checking if it can be positioned
done=0;
trial_counter=1;
while (done~=1)
x(k)=rand(1)*1000;
y(k)=rand(1)*1000;
thisX = x(k); % Get a trial point.
thisY = y(k);
% See how far is is away from existing keeper points.
distances = sqrt((thisX-keeperX).^2 + (thisY - keeperY).^2);
minDistance = min(distances);
if minDistance >= minAllowableDistance
keeperX(k) = thisX;
keeperY(k) = thisY;
done=1;
trial_counter=trial_counter+1;
counter = counter + 1;
end
if (trial_counter>2)
done=1;
end
end
end
end
所以这段代码工作正常,但有时如果点数超过 600,matlab 会冻结。问题已满,并且不再添加点数,因此 matlab 一遍又一遍地完成工作。所以我需要找到一种方法,当 trial_counter 大于 2 时,找到一个空的 space 并在那里定居。
trial_counter用于第三次不合适的扣分。 谢谢
由于 trial_counter=trial_counter+1;
仅在 if minDistance >= minAllowableDistance
内部调用,如果 minDistance < minAllowableDistance
很容易进入无限循环(例如,如果您现有的点非常紧凑)。
如何执行此操作取决于您的局限性,但如果您正在查看设定范围内的整数点,一种可能性是将这些点保留为二进制图像,并使用 bwdist
来算出距离变换,然后选择一个可接受的点。因此每次迭代都是(其中 BW
是您存储的 "image"/2D 二进制矩阵,其中 1 是所选点):
D = bwdist(BW);
maybe_points = find(D>minAllowableDistance); % list of possible locations
n = randi(length(maybe_points)); % pick one location
BW(maybe_points(n))=1; % add it to your matrix
(然后添加一些检查,如果找不到任何允许的点,则循环退出)