From: Roger Stafford on 24 Jun 2010 22:28 "dhaskantha " <dharen10(a)gmail.com> wrote in message <i00qvf$16s$1(a)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
From: James Tursa on 2 Jul 2010 17:10 "Roger Stafford" <ellieandrogerxyzzy(a)mindspring.com.invalid> wrote in message <i0147o$k3c$1(a)fred.mathworks.com>... > "dhaskantha " <dharen10(a)gmail.com> wrote in message <i00qvf$16s$1(a)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
First
|
Prev
|
Pages: 1 2 Prev: Plotting 3D of a function with circular domain Next: plotting 2-part function |