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
M.Prasanna kumar
M.Prasanna kumar 2018년 12월 14일
suppose x & y ranges from 1 to 300 , let number of random coordinates N = 5000 or more, is it possible to get 5000 or more (x,y) coordinate points which are equidistant to each other (here it is 20). my doubt is there is limit of 300 for x and y values. In the limited domain (area) . I'm thankful to your answer. but for the same code N = 500.
Error using randperm
K must be less than or equal to N.
Error in randmnum (line 27)
p = p(randperm(numel(p),1));)
above error is getting.
so doubt have raised in my mind

댓글을 달려면 로그인하십시오.

채택된 답변

Image Analyst
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);
0001 Screenshot.png
I'm not aware of an analytical statistical answer, so above we solve it numerically.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by