I need x,y coordinates which are randomly generated between 1 to 400 for x coordinates and 1 to 100 for y coordinates, condition is minimum distance between coordinates is 10 and maximum distance is 20, distances should vary from 10 to 20? plz help
3 ビュー (過去 30 日間)
古いコメントを表示
M.Prasanna kumar
2019 年 1 月 7 日
コメント済み: M.Prasanna kumar
2019 年 1 月 7 日
clear all;
close all;
pointsToPlace = 30 ; % # of random coordinates we need to place
% Preallocate points
x = zeros(1, pointsToPlace);
y = zeros(1, pointsToPlace);
loopCounter = 1;
maxIterations = 200000; % Number of tries before giving up.
numberPlaced = 0; % No points placed yet.
while numberPlaced < pointsToPlace && loopCounter < maxIterations
% Get new coordinate
xProposed = round((10 + (50-1)*rand()),0); %% random X- coordinates
yProposed = round((10 + (50-1)*rand()),0); %% 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 > 8)
numberPlaced = numberPlaced + 1;
x(numberPlaced) = xProposed;
y(numberPlaced) = yProposed;
end
end
loopCounter = loopCounter + 1;
end
above code (not my code) generates coordinates at equal distances, in my case distances should vary
0 件のコメント
採用された回答
Walter Roberson
2019 年 1 月 7 日
change
if min(distances > 8)
to
mind = min(distances);
if mind >= 10 && mind <= 20
その他の回答 (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!