6

enter image description herehello i want to ask a question how to make a circle in matlab and mark its center and generate a certain number of random points inside it for example 50 ? i know this code to make a circle

x = linspace(-sqrt(10),sqrt(10));
y1 = sqrt(10-x.^2);
y2 = -sqrt(10-x.^2);
plot(x,y1,x,y2)
axis equal
hold on

but i don't know how to generate 50 random points inside it then i thought of this pseudo code but i don't know how to write it in matlab

01: FOR all nodes j
 02: FOR all nodes i except node j 
03: IF distance(j to center) < distance(j to  i) AND
 04: distance(i to cell center) < distance(j to  i) 
05: THEN there's a red link from node i to node j 
06: ELSEIF distance(j to cell center) < distance(j to  i)
 07: THEN there's a blue link from node i to node j 
08: ELSE there's no D2D link from node i and j;
 09: node i has a green link with the base station 
10: END if 
11: END inner for-loop
user3482135
  • 75
  • 1
  • 7
  • I don't understand your question. The peudo-code defines a link set on a defined set of nodes, it does not generate a random set of nodes. – Daniel Mar 31 '14 at 17:39
  • Do you want the points to be uniformly distributed in the circle? – pjs Mar 31 '14 at 17:40
  • just an aside, you can generate random points in a square that contains the circle and choose only those of fall in the circle... – bla Mar 31 '14 at 17:47
  • i want it like the picture in my post i uploaded it now – user3482135 Mar 31 '14 at 18:20

3 Answers3

5

Think this is what you need -

%%// Plot the circle
x = linspace(-sqrt(10),sqrt(10));
y1 = sqrt(10-x.^2);
y2 = -sqrt(10-x.^2);
plot(x,y1,x,y2)
axis equal

%%// Choose from 1000 random point pairs
N = 1000; 
%%// Radius of circle
radius = sqrt(10); 

%%// Create a random point matrix Nx2
points_mat = [ radius*2*(rand(N,1)-0.5) radius*2*(rand(N,1)-0.5)];

%%// Select the first 50 pairs that lies inside circle
ind1 = find(sqrt( points_mat(:,1).^2 + points_mat(:,2).^2 )<radius);
points_mat=points_mat(ind1(1:50),:);

%%// Plot the 50 points on the circle
hold on
text(0,0,'x Center') %%// Center
text(points_mat(:,1),points_mat(:,2),'o') %%// 50 points

Plot

enter image description here

Divakar
  • 218,885
  • 19
  • 262
  • 358
  • thank u so much,so do you know after this how can i measure the distance between every point in the circle and the rest of the points and every point and the center like this pseudo code 01: FOR all nodes j 02: FOR all nodes i except node j 03: IF distance(j to center) < distance(j to i) AND 04: distance(i to cell center) < distance(j to i) 05: THEN there's a red link from node i to node j 06: ELSEIF distance(j to cell center) < distance(j to i) 07: THEN there's a blue link from node i to node j 08: else node i has a green link with the center 09: END if 10: END inner for-loop – user3482135 Mar 31 '14 at 17:49
  • 2
    Look into `pdist2`. It does exactly that. More info here - http://www.mathworks.in/help/stats/pdist2.html – Divakar Mar 31 '14 at 17:51
4

I don't know matlab, so I can't help you there, but if you want to do this without rejection you can generate the points in polar coordinates. If rand() returns a Uniform(0,1) random number, then:

r = radius * sqrt(rand())
theta = 2 * Pi * rand()
x = r * cos(theta)
y = r * sin(theta)

will yield values which are uniformly distributed within a circle of radius radius. Note the square root on the calculation of r, which adjusts the distribution of distance from the center of the circle so that the number of points at a given distance is always proportional to the area and hence is uniform. For spherical uniformity you'd take the cube root to keep proportionality to the volume, and in general the kth root for a k-dimensional hypersphere.

pjs
  • 18,696
  • 4
  • 27
  • 56
  • Those points will not be uniformly distribueted in the circle. Their radial and angular positions will be uniformly distributed, but since the area element is r*(dtheta)(dr) there is more area at a large radius so you need to choose more points at a large radius to end up with a uniform distribution in space. More here: http://stackoverflow.com/questions/5837572/generate-a-random-point-within-a-circle-uniformly – Doug Lipinski Mar 31 '14 at 19:06
  • I missed the `sqrt` in there, very sorry about that. My mistake. Unfortunately it looks like my downvote is locked now unless the posted is edited :( You might consider editing to add your additional explanation and I'll gladly switch to an upvote. – Doug Lipinski Mar 31 '14 at 22:18
  • @DougLipinski Explanations are good, I should have put one in before so thanks for the suggestion. – pjs Mar 31 '14 at 22:54
0

Here's another option:

%// Set parameters
R = 0.5;   %// radius
C = [3 4]; %// center [x y]
N = 50;    %// number of points inside circle

%// generate circle boundary
t = linspace(0, 2*pi, 100);
x = R*cos(t) + C(1);
y = R*sin(t) + C(2);

%// generate random points inside it
th = 2*pi*rand(N,1);
r  = R*rand(N,1);

xR = r.*cos(th) + C(1);
yR = r.*sin(th) + C(2);

%// Plot everything
figure(1), clf, hold on
plot(x,y,'b')
plot(C(1),C(2),'r.', 'MarkerSize', 100)
plot(xR,yR,'k.')
axis equal

enter image description here

Here's why that could be useful:

%// Set parameters
R = 0.5;     N = 50;
C = [3 4];   M = 100;  %// points on boundary

%// generate all points at once
t  = linspace(0, 2*pi, M)';
th = 2*pi*rand(N,1);
r  = R*rand(N,1);
xR = [R*ones(M,1); r] .* cos([t; th]) + C(1);
yR = [R*ones(M,1); r] .* sin([t; th]) + C(2);

%// Plot everything
figure(1), clf, hold on
plot(xR(1:M),yR(1:M),'b')                %// circle boundary
plot(C(1),C(2),'r.', 'MarkerSize', 100)  %// center
plot(xR(M+1:end),yR(M+1:end),'k.')       %// random points
axis equal
Rody Oldenhuis
  • 37,726
  • 7
  • 50
  • 96
  • thank u so much,do you know after this how can i measure the distance between every point in the circle and the rest of the points and every point and the center like the pic above and pseudo code a 01: FOR all points j 02: FOR all points i except point j 03: IF distance(j to center) < distance(j to i) AND 04: distance(i to cell center) < distance(j to i) 05: THEN there's a red link from node i to node j 06: ELSEIF distance(j to cell center) < distance(j to i) 07: THEN there's a blue link from node i to node j 08: else node i has a green link with the center 09: END if 10: END inner for-loop – user3482135 Mar 31 '14 at 19:33