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:
Array of random numbers with upper and lower limits

Subject: Array of random numbers with upper and lower limits

From: Ulrik Nash

Date: 6 Jan, 2010 13:44:04

Message: 1 of 6

Hi Everyone!

To create an array of 10 random numbers drawn from a Normal Distribution with mean equal to 0 and standard deviation equal to 1 the following can of course be used:

randn(10,1)

My question is, suppose that the minimum value allowed in each of the 10 instances is -0.5, say, and the maximum allowed value is 0.75, say, what code would efficiently ensure such a restriction?

Regards,

Ulrik.

Subject: Array of random numbers with upper and lower limits

From: Steven Lord

Date: 6 Jan, 2010 14:20:30

Message: 2 of 6


"Ulrik Nash" <uwn@sam.sdu.dk> wrote in message
news:hi2434$i2s$1@fred.mathworks.com...
> Hi Everyone!
>
> To create an array of 10 random numbers drawn from a Normal Distribution
> with mean equal to 0 and standard deviation equal to 1 the following can
> of course be used:
>
> randn(10,1)
>
> My question is, suppose that the minimum value allowed in each of the 10
> instances is -0.5, say, and the maximum allowed value is 0.75, say, what
> code would efficiently ensure such a restriction?

Do you want to impose these bounds while maintaining the mean of 0 and
standard deviation of 1? If so, I think it's impossible.

Wikipedia defines "standard deviation" informally as:

"It may be thought of as the average difference of the scores from the mean
of distribution, how far they are away from the mean."

http://en.wikipedia.org/wiki/Standard_deviation

If you generated numbers from this truncated distribution with mean 0, the
MAXIMUM difference between a generated random number and the mean would be
0.75, so the AVERAGE difference (i.e. the standard deviation) can't be more
than that.

So which of the requirements will you relax? The bounds, the mean, and/or
the standard deviation?

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: Array of random numbers with upper and lower limits

From: Ulrik Nash

Date: 6 Jan, 2010 14:33:02

Message: 3 of 6

"Steven Lord" <slord@mathworks.com> wrote in message <hi2676$dmi$1@fred.mathworks.com>...
>
> "Ulrik Nash" <uwn@sam.sdu.dk> wrote in message
> news:hi2434$i2s$1@fred.mathworks.com...
> > Hi Everyone!
> >
> > To create an array of 10 random numbers drawn from a Normal Distribution
> > with mean equal to 0 and standard deviation equal to 1 the following can
> > of course be used:
> >
> > randn(10,1)
> >
> > My question is, suppose that the minimum value allowed in each of the 10
> > instances is -0.5, say, and the maximum allowed value is 0.75, say, what
> > code would efficiently ensure such a restriction?
>
> Do you want to impose these bounds while maintaining the mean of 0 and
> standard deviation of 1? If so, I think it's impossible.
>
> Wikipedia defines "standard deviation" informally as:
>
> "It may be thought of as the average difference of the scores from the mean
> of distribution, how far they are away from the mean."
>
> http://en.wikipedia.org/wiki/Standard_deviation
>
> If you generated numbers from this truncated distribution with mean 0, the
> MAXIMUM difference between a generated random number and the mean would be
> 0.75, so the AVERAGE difference (i.e. the standard deviation) can't be more
> than that.
>
> So which of the requirements will you relax? The bounds, the mean, and/or
> the standard deviation?
>
> --
> Steve Lord
> slord@mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
>
Hi Steve, very good point. I should be more precise.

The effect I want can be conceived as a two step process:

1. Generate the array randn(10,1)
2. For each number in the array that is below minimum threshold, set number to minimum threshold, else leave be.
3. For each number in the array that is above maximum threshold, set number to maximum threshold, else leave be.

My question is, what is the most efficient way to accomplish this in Matlab?

Regards,

Ulrik.

Subject: Array of random numbers with upper and lower limits

From: Andy

Date: 6 Jan, 2010 14:40:23

Message: 4 of 6

"Ulrik Nash" <uwn@sam.sdu.dk> wrote in message <hi26uu$3cj$1@fred.mathworks.com>...
> "Steven Lord" <slord@mathworks.com> wrote in message <hi2676$dmi$1@fred.mathworks.com>...
> >
> > "Ulrik Nash" <uwn@sam.sdu.dk> wrote in message
> > news:hi2434$i2s$1@fred.mathworks.com...
> > > Hi Everyone!
> > >
> > > To create an array of 10 random numbers drawn from a Normal Distribution
> > > with mean equal to 0 and standard deviation equal to 1 the following can
> > > of course be used:
> > >
> > > randn(10,1)
> > >
> > > My question is, suppose that the minimum value allowed in each of the 10
> > > instances is -0.5, say, and the maximum allowed value is 0.75, say, what
> > > code would efficiently ensure such a restriction?
> >
> > Do you want to impose these bounds while maintaining the mean of 0 and
> > standard deviation of 1? If so, I think it's impossible.
> >
> > Wikipedia defines "standard deviation" informally as:
> >
> > "It may be thought of as the average difference of the scores from the mean
> > of distribution, how far they are away from the mean."
> >
> > http://en.wikipedia.org/wiki/Standard_deviation
> >
> > If you generated numbers from this truncated distribution with mean 0, the
> > MAXIMUM difference between a generated random number and the mean would be
> > 0.75, so the AVERAGE difference (i.e. the standard deviation) can't be more
> > than that.
> >
> > So which of the requirements will you relax? The bounds, the mean, and/or
> > the standard deviation?
> >
> > --
> > Steve Lord
> > slord@mathworks.com
> > comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
> >
> Hi Steve, very good point. I should be more precise.
>
> The effect I want can be conceived as a two step process:
>
> 1. Generate the array randn(10,1)
> 2. For each number in the array that is below minimum threshold, set number to minimum threshold, else leave be.
> 3. For each number in the array that is above maximum threshold, set number to maximum threshold, else leave be.
>
> My question is, what is the most efficient way to accomplish this in Matlab?
>
> Regards,
>
> Ulrik.

v=randn(10,1); % the data
a=-0.5; % the minimum
b=0.75; % the max
v(v<a)=a;
v(v>b)=b;
% note: this will affect your mean and standard deviation, obviously

Subject: Array of random numbers with upper and lower limits

From: Ulrik Nash

Date: 6 Jan, 2010 14:43:02

Message: 5 of 6

"Ulrik Nash" <uwn@sam.sdu.dk> wrote in message <hi26uu$3cj$1@fred.mathworks.com>...
> "Steven Lord" <slord@mathworks.com> wrote in message <hi2676$dmi$1@fred.mathworks.com>...
> >
> > "Ulrik Nash" <uwn@sam.sdu.dk> wrote in message
> > news:hi2434$i2s$1@fred.mathworks.com...
> > > Hi Everyone!
> > >
> > > To create an array of 10 random numbers drawn from a Normal Distribution
> > > with mean equal to 0 and standard deviation equal to 1 the following can
> > > of course be used:
> > >
> > > randn(10,1)
> > >
> > > My question is, suppose that the minimum value allowed in each of the 10
> > > instances is -0.5, say, and the maximum allowed value is 0.75, say, what
> > > code would efficiently ensure such a restriction?
> >
> > Do you want to impose these bounds while maintaining the mean of 0 and
> > standard deviation of 1? If so, I think it's impossible.
> >
> > Wikipedia defines "standard deviation" informally as:
> >
> > "It may be thought of as the average difference of the scores from the mean
> > of distribution, how far they are away from the mean."
> >
> > http://en.wikipedia.org/wiki/Standard_deviation
> >
> > If you generated numbers from this truncated distribution with mean 0, the
> > MAXIMUM difference between a generated random number and the mean would be
> > 0.75, so the AVERAGE difference (i.e. the standard deviation) can't be more
> > than that.
> >
> > So which of the requirements will you relax? The bounds, the mean, and/or
> > the standard deviation?
> >
> > --
> > Steve Lord
> > slord@mathworks.com
> > comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
> >
> Hi Steve, very good point. I should be more precise.
>
> The effect I want can be conceived as a two step process:
>
> 1. Generate the array randn(10,1)
> 2. For each number in the array that is below minimum threshold, set number to minimum threshold, else leave be.
> 3. For each number in the array that is above maximum threshold, set number to maximum threshold, else leave be.
>
> My question is, what is the most efficient way to accomplish this in Matlab?
>
> Regards,
>
> Ulrik.

Well there goes precision! I meant three (3) step process.

Subject: Array of random numbers with upper and lower limits

From: Jos (10584)

Date: 6 Jan, 2010 15:14:04

Message: 6 of 6

"Ulrik Nash" <uwn@sam.sdu.dk> wrote in message <hi27hm$942$1@fred.mathworks.com>...
> "Ulrik Nash" <uwn@sam.sdu.dk> wrote in message <hi26uu$3cj$1@fred.mathworks.com>...
> > "Steven Lord" <slord@mathworks.com> wrote in message <hi2676$dmi$1@fred.mathworks.com>...
> > >
> > > "Ulrik Nash" <uwn@sam.sdu.dk> wrote in message
> > > news:hi2434$i2s$1@fred.mathworks.com...
> > > > Hi Everyone!
> > > >
> > > > To create an array of 10 random numbers drawn from a Normal Distribution
> > > > with mean equal to 0 and standard deviation equal to 1 the following can
> > > > of course be used:
> > > >
> > > > randn(10,1)
> > > >
> > > > My question is, suppose that the minimum value allowed in each of the 10
> > > > instances is -0.5, say, and the maximum allowed value is 0.75, say, what
> > > > code would efficiently ensure such a restriction?
> > >
> > > Do you want to impose these bounds while maintaining the mean of 0 and
> > > standard deviation of 1? If so, I think it's impossible.
> > >
> > > Wikipedia defines "standard deviation" informally as:
> > >
> > > "It may be thought of as the average difference of the scores from the mean
> > > of distribution, how far they are away from the mean."
> > >
> > > http://en.wikipedia.org/wiki/Standard_deviation
> > >
> > > If you generated numbers from this truncated distribution with mean 0, the
> > > MAXIMUM difference between a generated random number and the mean would be
> > > 0.75, so the AVERAGE difference (i.e. the standard deviation) can't be more
> > > than that.
> > >
> > > So which of the requirements will you relax? The bounds, the mean, and/or
> > > the standard deviation?
> > >
> > > --
> > > Steve Lord
> > > slord@mathworks.com
> > > comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
> > >
> > Hi Steve, very good point. I should be more precise.
> >
> > The effect I want can be conceived as a two step process:
> >
> > 1. Generate the array randn(10,1)
> > 2. For each number in the array that is below minimum threshold, set number to minimum threshold, else leave be.
> > 3. For each number in the array that is above maximum threshold, set number to maximum threshold, else leave be.
> >
> > My question is, what is the most efficient way to accomplish this in Matlab?
> >
> > Regards,
> >
> > Ulrik.
>
> Well there goes precision! I meant three (3) step process.

... but it can done in a two-step process:

x = randn(10,1) % step 1: generate numbers
x = max(min(x,0.75),-0.5) % restrict to boundaries

hth
Jos

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