필터 지우기
필터 지우기

Generate Random position that are not duplicate

조회 수: 6 (최근 30일)
Riccardo Giaffreda
Riccardo Giaffreda 2020년 2월 27일
댓글: Adam Danz 2021년 12월 4일
Hello, I want to randomly chose N vector of two coordinates each (x and y, so N positions with thier coordinates) inside a circular area and then approximate the coordinates to its nearest integer (to have only positions such as 34;20 or 6;18 but not 6,27 ; 17,98). I did this with this code but I need the positions not to replicate (so I can't have two positions 10;15 for example). How can I do it?
N=30;
Radius= 50
x0 = 0; %Center of the cell in x direction
y0 = 0; %Center of the cell in y direction
%Set of Points
t = 2*pi*rand(N,1);
r = Radius*sqrt(rand(N,1));
x = x0 + r.*cos(t);
y = y0 + r.*sin(t);
x=round(x);
y=round(y);

답변 (2개)

Temu
Temu 2020년 2월 27일
Hi Riccardo,
I would go for rejection sampling: just keep on generating until you have enough points.
N = 30;
Radius = 5;
%Set of Points
x1 = [];
while( size( x1, 1 ) < N )
x1 = [x1; round( Radius .* ( rand( N, 1 ) * 2 - 1 )) + ...
1j .* round( Radius .* ( rand( N, 1 ) * 2 - 1 ))];
[~,ind1] = unique( x1 );
x1 = x1(ind1);
end
x1 = x1(1:N);
x = real( x1 );
y = imag( x1 );
hth,
Temu

Adam Danz
Adam Danz 2020년 2월 27일
편집: Adam Danz 2020년 3월 6일
This efficient solution takes the following steps.
  1. List all integer coordinates within the circle
  2. Randomly choose N of those coordinates, without replacement (requires Stats & Machine Learning toolbox)
See inline comments for details.
% Set up inputs
N=30;
Radius= 50
x0 = 0; %Center of the cell in x direction
y0 = 0; %Center of the cell in y direction
% get all integer coordinates inside the square that frames the circle
xVec = floor(x0-Radius) : ceil(x0+Radius);
yVec = floor(y0-Radius) : ceil(y0+Radius);
[xGrid,yGrid] = meshgrid(xVec, yVec);
% Eliminate coodinates outside of the circle
xy = [xGrid(:), yGrid(:)];
xyDist = pdist2(xy,[x0,y0]);
xy(xyDist > Radius, :) = []; % Use >= if you want to exclude points on circumf.
% xy now lists all integer coordinates inside the circle.
% For visual inspection:
clf();
plot(xy(:,1),xy(:,2), 'k.');
axis equal
% Now, select a random sample (N) without replication
% Requires Stats & Machine Learning Toolbox
xySelect = datasample(xy, N, 'Replace', false);
% Add chosen coordinates to the plot
hold on
plot(xySelect(:,1),xySelect(:,2), 'ro')
The image below shows all possible integer coordinates within the cirlce (black dots) and the N randomly chosen coordinates (red circles).
  댓글 수: 2
omar th
omar th 2021년 12월 4일
how can I determine the position of each random points ?
Adam Danz
Adam Danz 2021년 12월 4일
If you plotted the points, you don't need to determine them since you've already got their coordinates.
If you didn't plot them and have the fig file, you can easily extract their coordinates from the file.
If this is a flat image (jpg, png, etc) then you can using image analysis tools to isolate the red points and the axis frame to estimate their position. Also see grabit from the file exchange.

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

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by