Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Generate random points on a circular sector

Subject: Generate random points on a circular sector

From: Valentina

Date: 6 May, 2012 16:01:08

Message: 1 of 9

Hi.

I am trying to generate random points in cartesian coordinates inside a circular sector defined by the radius R and the angle w (I suppose the center in (0,0) and that the angle is in [-w,w]).

If anyone has a sample code or can help me with this, thanks for the help.

Subject: Generate random points on a circular sector

From: Nasser M. Abbasi

Date: 6 May, 2012 16:22:36

Message: 2 of 9

On 5/6/2012 11:01 AM, Valentina wrote:
> Hi.
>
> I am trying to generate random points in cartesian coordinates inside a
>circular sector defined by the radius R and the angle w (I suppose the
>center in (0,0) and that the angle is in [-w,w]).
>
> If anyone has a sample code or can help me with this, thanks for the help.

may be

------------------------
clear all; close all;

angle = @(w) -w+2*w*rand();
rad = @(r) r*rand();

N = 30; %how many points
w = pi/4; %range of anlge [-w,w]
r = 1; %radius

points = arrayfun(@(i) [angle(w) rad(r) ],1:N, 'UniformOutput',false);
points = cell2mat(points(:));
polar(points(:,1),points(:,2),'or')

-----------------------------------------

--Nasser

Subject: Generate random points on a circular sector

From: Bruno Luong

Date: 6 May, 2012 16:36:08

Message: 3 of 9

"Valentina" wrote in message <jo6784$eqn$1@newscl01ah.mathworks.com>...
> Hi.
>
> I am trying to generate random points in cartesian coordinates inside a circular sector defined by the radius R and the angle w (I suppose the center in (0,0) and that the angle is in [-w,w]).
>
> If anyone has a sample code or can help me with this, thanks for the help.

% Generate n points uniformly in the polar region (r,theta) in (0,1) x (0,w)
n=1e4;
w = pi/3;
r=sqrt(rand(1,n));
theta=rand(1,n)*w;
x=r.*cos(theta);
y=r.*sin(theta);

plot(x,y,'.')
axis equal

% Bruno

Subject: Generate random points on a circular sector

From: Bruno Luong

Date: 6 May, 2012 16:38:08

Message: 4 of 9

"Nasser M. Abbasi" <nma@12000.org> wrote in message <jo68g9$aj5$1@speranza.aioe.org>...

>
> may be
>

No, since your code does not generate uniform points.

Bruno

Subject: Generate random points on a circular sector

From: Nasser M. Abbasi

Date: 6 May, 2012 17:05:23

Message: 5 of 9

On 5/6/2012 11:22 AM, Nasser M. Abbasi wrote:
> On 5/6/2012 11:01 AM, Valentina wrote:
>> Hi.
>>
>> I am trying to generate random points in cartesian coordinates inside a
>> circular sector defined by the radius R and the angle w (I suppose the
>> center in (0,0) and that the angle is in [-w,w]).
>>
>> If anyone has a sample code or can help me with this, thanks for the help.
>
> may be
>
> ------------------------
> clear all; close all;
>
> angle = @(w) -w+2*w*rand();
> rad = @(r) r*rand();
>
> N = 30; %how many points
> w = pi/4; %range of anlge [-w,w]
> r = 1; %radius
>
> points = arrayfun(@(i) [angle(w) rad(r) ],1:N, 'UniformOutput',false);
> points = cell2mat(points(:));
> polar(points(:,1),points(:,2),'or')
>
> -----------------------------------------
>
> --Nasser

Ok, here is a version without arrayfun (I know some here do not
like arrayfun :)

-------------------------------
N = 30; %how many points
w = pi/4; %range of anlge [-w,w]
r = 1; %radius

angle = -w+2*w*rand(N,1);
rad = r*rand(N,1);
polar(angle,rad,'or')
----------------------------

--Nasser

Subject: Generate random points on a circular sector

From: Roger Stafford

Date: 6 May, 2012 17:42:08

Message: 6 of 9

"Nasser M. Abbasi" <nma@12000.org> wrote in message <jo6b0h$h18$1@speranza.aioe.org>...
> N = 30; %how many points
> w = pi/4; %range of anlge [-w,w]
> r = 1; %radius
> angle = -w+2*w*rand(N,1);
> rad = r*rand(N,1);
> polar(angle,rad,'or')
- - - - - - - -
  Nasser, what Bruno is objecting is that without the square root operation on the radius calculation the distribution of points you will get is not uniform area-wise. If you take a careful look at the results of your own code with a large N, say N = 1000, you will see that the points are much more concentrated at the circle's center than at its circumference. Use "rad = r*sqrt(rand(N,1))" instead and you can easily see the difference.

Roger Stafford

Subject: Generate random points on a circular sector

From: Greg Heath

Date: 6 May, 2012 18:24:57

Message: 7 of 9

On May 6, 12:01 pm, "Valentina " <vale_dr...@hotmail.it> wrote:
> Hi.
>
> I am trying to generate random points in cartesian coordinates inside a circular sector defined by the radius R and the angle w (I suppose the center in (0,0) and that the angle is in [-w,w]).
>
> If anyone has a sample code or can help me with this, thanks for the help.

dx*dy = 0.5*d(r^2)*d(theta)

Therefore generate r^2 and theta using the uniform distribution rand.

Hope this help.

Greg

Subject: Generate random points on a circular sector

From: Valentina

Date: 6 May, 2012 22:26:08

Message: 8 of 9

Perfect, thanks to all!

Valentina

Subject: Generate random points on a circular sector

From: Valentina

Date: 6 May, 2012 22:28:08

Message: 9 of 9

Perfect. Thank you so much!

Valentina

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us