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 09:16:02 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 38
Message-ID: <gdmr0i$kd1$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-02-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1224666962 20897 172.30.248.37 (22 Oct 2008 09:16:02 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Wed, 22 Oct 2008 09:16:02 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 870065
Xref: news.mathworks.com comp.soft-sys.matlab:496707


Adshak <adshaikh.hipnet@googlemail.com> wrote in message ...
> In code, it might look like this:
> >
> > W =3D [] ;
> > for i=3D1:numel(V),
> > =A0 =A0while rand < p(V(i))
> > =A0 =A0 =A0 =A0W =3D [W V(i)] ;
> > =A0 =A0end
> > end
> >
> > so you might end up with
> >
> > W =3D [], W =3D 1, W =3D [2 2 3 3 1], or W =3D [2 2 2 2 3 3 1 1 1 1 2]
> >
> > Am I correct?
...
> No, the only solution possible is the last possibility of W as in, if
> W were the output vector, the elements in V need to exist as it is but
> need to be replicated by the side of the existence of the original
> element. The least possibility is that the output vector should be the
> original input vector itself.

So this would do then:

V = [2 3 1 2] ;
p = [0.2 0.3 0.5] ;
W = [] ;
for i=1:numel(V),
  W = [W V(i)] ;
  while rand < p(V(i))
    W = [W V(i)] ;
  end
end
W

So at least all elements of V are present in W.

Jos