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:
Help with randn function

Subject: Help with randn function

From: Kevin Ellis

Date: 22 Jan, 2012 19:36:30

Message: 1 of 6

Hello,

I need to generate an array with numbers within the range [-1:1]. I would like to use the built in function "randn" to build the array. For example, in my M-File I write

A = randn(1,20);

However, some elements of the Array A are not within the range [-1:1]. I have tried to use while loops to check each element and try to keep it within the range, but this isn't working very well.

Is there a way to designate a range for the built in function "randn"?

Thanks.

Kevin

Subject: Help with randn function

From: dpb

Date: 22 Jan, 2012 20:08:18

Message: 2 of 6

On 1/22/2012 1:36 PM, Kevin Ellis wrote:
> Hello,
>
> I need to generate an array with numbers within the range [-1:1]. I
> would like to use the built in function "randn" to build the array. ...
...
>
> Is there a way to designate a range for the built in function "randn"?
...

Not other than by scaling after the fact. Since the underlying
distribution of randn() is based on the normal distribution which is
unbounded, using it as a source distribution for limiting values to some
range is suspect choice at best.

Why did you choose it over, say rand() or some other distribution that
_is_ defined over a range?

--

Subject: Help with randn function

From: Francesco

Date: 22 Jan, 2012 21:51:11

Message: 3 of 6

dpb <none@non.net> wrote in message <jfhqbi$sab$1@speranza.aioe.org>...
> On 1/22/2012 1:36 PM, Kevin Ellis wrote:
> > Hello,
> >
> > I need to generate an array with numbers within the range [-1:1]. I
> > would like to use the built in function "randn" to build the array. ...
> ...
> >
> > Is there a way to designate a range for the built in function "randn"?
> ...
>
> Not other than by scaling after the fact. Since the underlying
> distribution of randn() is based on the normal distribution which is
> unbounded, using it as a source distribution for limiting values to some
> range is suspect choice at best.
>
> Why did you choose it over, say rand() or some other distribution that
> _is_ defined over a range?
>
> --

Another way you have is to call normrnd in this way:

normrnd(-1,1,1,20)

I hope this helps.

Subject: Help with randn function

From: TideMan

Date: 23 Jan, 2012 00:52:09

Message: 4 of 6

On Jan 23, 10:51 am, "Francesco " <fra041...@hotmail.it> wrote:
>
> Another way you have is to call normrnd in this way:
>
> normrnd(-1,1,1,20)
>
> I hope this helps.

No, it doesn't help, because it is wrong.
The first 2 arguments are the mean and standard deviation.

A possible way of getting the distribution between -1 and 1 is to use
the universal threshold of Donoho & Johnstone:
lambda=sqrt(2*log(n))*sigma;
which gives the expected maximum of randn for n numbers.
Note that it's only the "expected" maximum, so this means lambda could
be exceeded.
But scaling the numbers by lambda will, on average, ensure they are
between -1 and 1.

Subject: Help with randn function

From: Francesco

Date: 23 Jan, 2012 13:55:10

Message: 5 of 6

TideMan <mulgor@gmail.com> wrote in message <82628bd5-8671-4083-8086-604d1b8fbecb@c8g2000yqc.googlegroups.com>...
> On Jan 23, 10:51 am, "Francesco " <fra041...@hotmail.it> wrote:
> >
> > Another way you have is to call normrnd in this way:
> >
> > normrnd(-1,1,1,20)
> >
> > I hope this helps.
>
> No, it doesn't help, because it is wrong.
> The first 2 arguments are the mean and standard deviation.
>
> A possible way of getting the distribution between -1 and 1 is to use
> the universal threshold of Donoho & Johnstone:
> lambda=sqrt(2*log(n))*sigma;
> which gives the expected maximum of randn for n numbers.
> Note that it's only the "expected" maximum, so this means lambda could
> be exceeded.
> But scaling the numbers by lambda will, on average, ensure they are
> between -1 and 1.

Yeah,
 
Tideman is right, I'm sorry!!

A solution would be calling rand and then scaling your results.
Check for Box-Muller transform as well.

Regards.

Subject: Help with randn function

From: Bruno Luong

Date: 23 Jan, 2012 14:09:09

Message: 6 of 6

"Kevin Ellis" wrote in message <jfhofu$l8u$1@newscl01ah.mathworks.com>...
> Hello,
>
> I need to generate an array with numbers within the range [-1:1]. I would like to use the built in function "randn" to build the array. For example, in my M-File I write
>
> A = randn(1,20);
>
> However, some elements of the Array A are not within the range [-1:1]. I have tried to use while loops to check each element and try to keep it within the range, but this isn't working very well.
>
> Is there a way to designate a range for the built in function "randn"?

This function will help:
http://www.mathworks.com/matlabcentral/fileexchange/23832-truncated-gaussian

Bruno

Tags for this Thread

No tags are associated with 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