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 06:50:04 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 80
Message-ID: <gdmies$g34$1@fred.mathworks.com>
References: <6fbe2615-a1f9-46ad-93d4-60500f097e38@q35g2000hsg.googlegroups.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1224658204 16484 172.30.248.37 (22 Oct 2008 06:50:04 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Wed, 22 Oct 2008 06:50:04 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 870065
Xref: news.mathworks.com comp.soft-sys.matlab:496686


Adshak <adshaikh.hipnet@googlemail.com> wrote in message <6fbe2615-a1f9-46ad-93d4-60500f097e38@q35g2000hsg.googlegroups.com>...
> 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
> 
> 

I am not sure if I understand your problem correctly. Let me try to rephrase. You have a vector:

V = [2 3 1 2]

with associated changes for the values 1, 2 and 3:

p = [.2 .3 .5] ;

and you want to expand each value in V an individual number of times, based on p. In code, it might look like this:

W = [] ;
for i=1:numel(V),
   while rand < p(V(i))
       W = [W V(i)] ;
   end
end

so you might end up with

W = [], W = 1, W = [2 2 3 3 1], or W = [2 2 2 2 3 3 1 1 1 1 2]

Am I correct?

Indeed, for long vectors V this might indeed take some time, but if I am correct, improvements can be made.

Jos