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:
How to make the output always be betweeb 0 and 1

Subject: How to make the output always be betweeb 0 and 1

From: Dharini Pathmanathan

Date: 24 Jun, 2010 04:08:21

Message: 1 of 9

Please help!
This is part of my script.

pb=[];
pb(1)=exp(-lam)

for k=1:n
pb(k+1)=(lam/k)*pb(k)
end

I want the values of pb to always be between 0 and 1. How do I specify this in my program?
Thank you.

Subject: How to make the output always be betweeb 0 and 1

From: Roger Stafford

Date: 24 Jun, 2010 04:39:04

Message: 2 of 9

"Dharini Pathmanathan" <dharen10@gmail.com> wrote in message <hvulnk$4uf$1@fred.mathworks.com>...
> Please help!
> This is part of my script.
>
> pb=[];
> pb(1)=exp(-lam)
>
> for k=1:n
> pb(k+1)=(lam/k)*pb(k)
> end
>
> I want the values of pb to always be between 0 and 1. How do I specify this in my program?
> Thank you.
- - - - - - - - -
  Provided that lam is positive, all the pb values should lie in (0,1). What you are asking is that

 pb(k+1) = lam^k/k!*exp(-lam) always lies in (0,1) for all k.

This is equivalent to

 lam^k/k! < exp(lam) = 1 + lam/1! + lam^2/2! + lam^3/3! + ...

which is clearly always true for positive lam.

  Request: Please don't enter duplicate threads. It is an inconvenience to those who are responding because it often causes an unnecessary duplication of their efforts.

Roger Stafford

Subject: How to make the output always be between 0 and 1

From: dhaskantha

Date: 24 Jun, 2010 09:10:22

Message: 3 of 9

Oh yes. This one is the recurrence formula for the Poisson pmf.
What if I happened to bump into cases such as:
for i=1:n
x(i)=normrnd(0,1);
end

where I only want the values of x(i), say between 0 and 1. How do I do it?
Thanks.

Subject: How to make the output always be between 0 and 1

From: Jan Simon

Date: 24 Jun, 2010 10:21:05

Message: 4 of 9

Dear dhaskantha,

> for i=1:n
> x(i)=normrnd(0,1);
> end
>
> where I only want the values of x(i), say between 0 and 1. How do I do it?

Between 0 and 1:
  x = min(0, max(1, anyFunction))
0 or 1:
  x = double(anyFunction > 0.5);
Obviously "0.5" is just an example and "anyFunction" depends on your needs.
anyFunction can reply (or be) an array also, which is usually faster than using a FOR loop for elementwise processing.
Care about NaNs, on demand.

Good luck, Jan

Subject: How to make the output always be between 0 and 1

From: dhaskantha

Date: 24 Jun, 2010 12:07:23

Message: 5 of 9

Thank you, Jan. I will try.

Subject: How to make the output always be between 0 and 1

From: Roger Stafford

Date: 24 Jun, 2010 16:39:04

Message: 6 of 9

"dhaskantha " <dharen10@gmail.com> wrote in message <hvvhpr$5nd$1@fred.mathworks.com>...
> Thank you, Jan. I will try.
- - - - - - - - -
  Rather than changing values which lie outside of [0,1] to a 0 or 1, from a statistical point of view it is probably preferable to simply reject them and repeat the call on normrnd or whatever your source is until you have a sufficient number of random values. Otherwise you may get a number of values that are artificially stacked up at exactly 0 or 1 and this would skew your statistical distribution. You can accomplish this with a while loop.

  Always remember that when you truncate a normal distribution in this manner, strictly speaking you no longer have a normal distribution, which is supposed to be free to extend to an unlimited amount either up or down. Both the mean and variance will be different from what you might expect.

Roger Stafford

Subject: How to make the output always be between 0 and 1

From: dhaskantha

Date: 24 Jun, 2010 23:50:08

Message: 7 of 9

Thank you. Roger. Could you show me an example of 20 generating random normal values between 0 and 1 using the while loop? Your help is much appreciated. I am not very familiar with the while loop.
Thank you, once again.


Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <i001n8$8or$1@fred.mathworks.com>...
> "dhaskantha " <dharen10@gmail.com> wrote in message <hvvhpr$5nd$1@fred.mathworks.com>...
> > Thank you, Jan. I will try.
> - - - - - - - - -
> Rather than changing values which lie outside of [0,1] to a 0 or 1, from a statistical point of view it is probably preferable to simply reject them and repeat the call on normrnd or whatever your source is until you have a sufficient number of random values. Otherwise you may get a number of values that are artificially stacked up at exactly 0 or 1 and this would skew your statistical distribution. You can accomplish this with a while loop.
>
> Always remember that when you truncate a normal distribution in this manner, strictly speaking you no longer have a normal distribution, which is supposed to be free to extend to an unlimited amount either up or down. Both the mean and variance will be different from what you might expect.
>
> Roger Stafford

Subject: How to make the output always be between 0 and 1

From: Roger Stafford

Date: 25 Jun, 2010 02:28:08

Message: 8 of 9

"dhaskantha " <dharen10@gmail.com> wrote in message <i00qvf$16s$1@fred.mathworks.com>...
> Thank you. Roger. Could you show me an example of 20 generating random normal values between 0 and 1 using the while loop? Your help is much appreciated. I am not very familiar with the while loop.
> Thank you, once again.
- - - - - - - - - - -
  I'll use the example you quoted of normrnd(0,1) with its mean 0 and variance 1, and choosing only values of it between 0 and 1. This would be a rather strange undertaking because more than half of the outputs of normrnd would be rejected this way. However it will show you the method of selection with a while-loop. As you see, the while-loop keeps repeating until it has received 20 of the random outputs that fall within range.

 N = 20; % The desired number of outputs of normrnd in [0,1]
 x = zeros(N,1); % The results are stored here
 n = 0; % The number already selected
 while n < N % Keep looping until N values in range have occurred
  t = normrnd(0,1);
  if 0<=t & t<=1 % Is it in range?
   n = n+1;
   x(n) = t; % If so, accept it
  end
 end % Exit when n is equal to N

  Please bear in mind that the resulting distribution in x would be a far cry from being normal! The entire left side and a great section of the right side of the standard "bell curve" would be missing.

Roger Stafford

Subject: How to make the output always be between 0 and 1

From: James Tursa

Date: 2 Jul, 2010 21:10:06

Message: 9 of 9

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <i0147o$k3c$1@fred.mathworks.com>...
> "dhaskantha " <dharen10@gmail.com> wrote in message <i00qvf$16s$1@fred.mathworks.com>...
> > Thank you. Roger. Could you show me an example of 20 generating random normal values between 0 and 1 using the while loop? Your help is much appreciated. I am not very familiar with the while loop.
> > Thank you, once again.
> - - - - - - - - - - -
> I'll use the example you quoted of normrnd(0,1) with its mean 0 and variance 1, and choosing only values of it between 0 and 1. This would be a rather strange undertaking because more than half of the outputs of normrnd would be rejected this way. However it will show you the method of selection with a while-loop. As you see, the while-loop keeps repeating until it has received 20 of the random outputs that fall within range.
>
> N = 20; % The desired number of outputs of normrnd in [0,1]
> x = zeros(N,1); % The results are stored here
> n = 0; % The number already selected
> while n < N % Keep looping until N values in range have occurred
> t = normrnd(0,1);
> if 0<=t & t<=1 % Is it in range?
> n = n+1;
> x(n) = t; % If so, accept it
> end
> end % Exit when n is equal to N

Or, for a somewhat vectorized version of this rejection scheme:

N = 20;
x = randn(N,1);
z = (x < 0) | (x > 1);
s = sum(z);
while s
    x(z) = randn(s,1);
    z = (x < 0) | (x > 1); % not optimized, doing extra work here
    s = sum(z);
end

James Tursa

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