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!