Generate Random position that are not duplicate
조회 수: 6 (최근 30일)
이전 댓글 표시
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);
댓글 수: 0
답변 (2개)
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
댓글 수: 0
Adam Danz
2020년 2월 27일
편집: Adam Danz
2020년 3월 6일
This efficient solution takes the following steps.
- List all integer coordinates within the circle
- 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).
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/273977/image.png)
댓글 수: 2
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 Center 및 File Exchange에서 Annotations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!