Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Growing elements in a vector
Date: Wed, 22 Oct 2008 20:00:03 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 45
Message-ID: <gdo0o3$4fl$1@fred.mathworks.com>
References: <6fbe2615-a1f9-46ad-93d4-60500f097e38@q35g2000hsg.googlegroups.com> <fb411cc8-4e3e-4940-8e3f-d460cd6e26c5@y71g2000hsa.googlegroups.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1224705603 4597 172.30.248.35 (22 Oct 2008 20:00:03 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Wed, 22 Oct 2008 20:00:03 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: news.mathworks.com comp.soft-sys.matlab:496778


Adshak <adshaikh.hipnet@googlemail.com> wrote in message 
> Dear Ray,
> I am sorry I do not have access to the book you just mentioned, 
> so it is difficult to understand your solution.
> .......
> adshak
------------
  Adshak, a vectorized and undoubtedly much faster method of generating the number of repetitions for each element in your original vector (which Jos has called M) is implicit in the formula given in Ray Coopman's second article in this thread.  You don't need to access the reference he has given there.

  Instead of generating an indefinitely long series of random numbers for each element of the vector, just use a single call on the 'rand' function.  Let p be the "comparison test" number (e.g. .2, .3, & .5) and let u be the value of a 'rand' call: u = rand.  Then Ray is telling you to find

 m = ceil(log(u)/log(p))

and use this as the number of repetitions for the corresponding vector element (1, 2, 3, etc.) all in a single step.

  The reasoning behind this is that if

 m = ceil(log(u)/log(p))

then

 m-1 < log(u)/log(p) <= m

and multiplying by the negative log(p) gives

 (m-1)*log(p) > log(u) >= m*log(p)

which by taking the exponential of all three quantities yields

 p^(m-1) > u >= p^m

The probability of this is thus

 p^(m-1)-p^m = p^(m-1)*(1-p)

which is the same as the probability of obtaining m-1 comparisons which are less than p followed by a final comparison that is greater or equal to p, and thus gives the desired number of repetitions for the corresponding vector element.

  You can of course make a vector equation out of the above 'ceil' expression which would give the vector M all in one vectorized line:

 M = ceil(log(rand(n,1)./log(P));

where P is the vector of comparison values and n is its length.

Roger Stafford