Generate Random position that are not duplicate

18 views (last 30 days)
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?
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);

Answers (2)

Temu on 27 Feb 2020
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);
x1 = x1(1:N);
x = real( x1 );
y = imag( x1 );

Adam Danz
Adam Danz on 27 Feb 2020
Edited: Adam Danz on 6 Mar 2020
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
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:
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).
omar th
omar th on 4 Dec 2021
how can I determine the position of each random points ?
Adam Danz
Adam Danz on 4 Dec 2021
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.

Sign in to comment.


Community Treasure Hunt

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

Start Hunting!