Path: news.mathworks.com!newsfeed-00.mathworks.com!nlpi057.nbdc.sbc.com!prodigy.net!news.glorb.com!postnews.google.com!b31g2000prf.googlegroups.com!not-for-mail
From: Ray Koopman <koopman@sfu.ca>
Newsgroups: comp.soft-sys.matlab,sci.stat.math
Subject: Re: Growing elements in a vector
Date: Tue, 21 Oct 2008 15:10:16 -0700 (PDT)
Organization: http://groups.google.com
Lines: 50
Message-ID: <8fbc8d86-748d-4cd7-96b8-4a3063c4c4db@b31g2000prf.googlegroups.com>
References: <6fbe2615-a1f9-46ad-93d4-60500f097e38@q35g2000hsg.googlegroups.com>
NNTP-Posting-Host: 70.68.170.218
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
X-Trace: posting.google.com 1224627017 21739 127.0.0.1 (21 Oct 2008 22:10:17 GMT)
X-Complaints-To: groups-abuse@google.com
NNTP-Posting-Date: Tue, 21 Oct 2008 22:10:17 +0000 (UTC)
Complaints-To: groups-abuse@google.com
Injection-Info: b31g2000prf.googlegroups.com; posting-host=70.68.170.218; 
	posting-account=xM691AkAAACRhg2rzU0Kd6hZjdls4krD
User-Agent: G2/1.0
X-HTTP-UserAgent: Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; 
	rv:1.8.1.17) Gecko/20080829 Firefox/2.0.0.17,gzip(gfe),gzip(gfe)
Xref: news.mathworks.com comp.soft-sys.matlab:496642 sci.stat.math:84900

On Oct 21, 2:10 pm, Adshak <adshaikh.hip...@googlemail.com> wrote:
> Dear All,
>
> I have a tricky little issue I need to deal with:
>
> I have a vector which goes something like this
>
> vector = [1 2 3 1 2 3 2 3 1 3 2 1 2 3 1 3 2 1 2 3 1 1 3 1 2 1 3 1 2 3
> 1...];
>
> Please note that the above vector will only have unique elements as
> neighbours and hence no repitition of elements.
>
> Now what i want is to cleverly with less computational burden,
> consider every element in the vector one by one and replicate the
> element as many times as it takes to pass a comparison test against a
> randomly drawn variable.
>
> For example, for every unique element 1, 2 and 3 I have a number
> between 0 and 1 i.e 0.2, 0.3 and 0.5 respectively.
>
> So how this would work is
>
> 1) Consider Vector(1), the element is 1.
> 2) Draw a Random number. Compare it to 0.2. If less than 0.2, append a
> 1 after the first 1, else move to next unique element. Continue
> appending 1's till random value generated is higher than 0.2
> 3) If in element 2, compare to 0.3 and if in element 3 compare to 0.5.
>
> finally my output vector should look something like this:
>
> outvector = [1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 3 3 3 3 3 1 1 1 1 1 1 1 1
> 1  2 2 2 2 2 2 3 3 3 3 3 3 3 3 1 1 1 1 3 3 3 3 3.......];
>
> Trying this with for loops for large vectors seems to take a toll on
> the speed of computation and I would prefer a quick vector based
> solution.
>
> I have been spending a lot of time on this to compute efficiently and
> will be really grateful for a solution for this with example code.
>
> Many thanks,
>
> Adshak

See Luc Devroye, Non-Uniform Random Variate Generation, p 87, example
2.2, The geometric distribution: Let the length of a run that begins
with i be ceiling(log(u)/log(1-p_i)), where u is a Uniform(0,1)
random number and p_i is the theoretical marginal probability of
observing i (e.g. in your example, p_1 = .2 , p_2 = .3, p_3 = .5).