for the same answer,,suppose, X ranges from 1 to 500, y ranges from 1 to 200 , Is it possible to find how many such coordinates are possible so that distance from each and every coordinate to other coordinate is > 20
1 回表示 (過去 30 日間)
古いコメントを表示
BWremain = true(300);
% Number of random coordinates
N = 100;
% Selected coordinates are stored these variables
row = nan(N,1);
col = nan(N,1);
for kk = 1:N
BW = false(300);
p = find(BWremain);
p = p(randperm(numel(p),1));
BW(p) = true;
BW = bwdist(BW) > 20;
BWremain = BWremain & BW;
[row(kk),col(kk)] = ind2sub(size(BWremain),p)
end
2 件のコメント
KSSV
2018 年 12 月 14 日
How it is different from this question? https://in.mathworks.com/matlabcentral/answers/435673-i-want-x-y-coordinates-which-are-randomly-generated-between-1-to-300-condition-is-distance-between
採用された回答
Image Analyst
2018 年 12 月 14 日
編集済み: Image Analyst
2018 年 12 月 14 日
The max number varies depending on what points were actually placed. Using numbers from your question, and trying a million coordinates, we can see that we can place from about 150 to 200 points. See code below:
pointsToPlace = 5000000 ; % # of random coordinates we need to place - some huge number
% Preallocate points
x = zeros(1, pointsToPlace);
y = zeros(1, pointsToPlace);
loopCounter = 1;
maxIterations = 1000000; % Number of tries before giving up.
numberPlaced = 0; % No points placed yet.
while numberPlaced < pointsToPlace && loopCounter <= maxIterations
% Get new coordinate
xProposed = 1 + (500-1)*rand(); %% random X- coordinates
yProposed = 1 + (200-1)*rand(); %% random Y- coordinates
if loopCounter == 1
% First one automatically gets added of course.
numberPlaced = 1;
x(numberPlaced) = xProposed;
y(numberPlaced) = yProposed;
else
% Compute distance to all prior coordinates.
distances = sqrt((xProposed - x(1:numberPlaced)) .^ 2 + (yProposed - y(1:numberPlaced)) .^2);
% If less than 20, add it
if min(distances > 20)
numberPlaced = numberPlaced + 1;
x(numberPlaced) = xProposed;
y(numberPlaced) = yProposed;
end
end
loopCounter = loopCounter + 1;
end
% Crop to how many we actually got.
x = x(1:numberPlaced);
y = y(1:numberPlaced);
fprintf('Placed %d points after %d iterations\n', numberPlaced, loopCounter-1);
plot(x, y, 'b*', 'LineWidth', 2, 'MarkerSize', 14);
grid on;
axis equal
xlim([0, 500]);
ylim([0, 200]);
xlabel('X', 'FontSize', 20);
ylabel('Y', 'FontSize', 20);
I'm not aware of an analytical statistical answer, so above we solve it numerically.
0 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Creating and Concatenating Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!