```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)
Lines: 50
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)
NNTP-Posting-Date: Tue, 21 Oct 2008 22:10:17 +0000 (UTC)
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

> 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,
>