|
On Oct 22, 7:50=A0am, "Jos " <DEL...@jasenDEL.nl> wrote:
> Adshak <adshaikh.hip...@googlemail.com> wrote in message <6fbe2615-a1f9-4=
6ad-93d4-60500f097...@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 =3D [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 =3D [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 =A02 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 rephr=
ase. You have a vector:
>
> V =3D [2 3 1 2]
>
> with associated changes for the values 1, 2 and 3:
>
> p =3D [.2 .3 .5] ;
>
> and you want to expand each value in V an individual number of times, bas=
ed on p. 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?
>
> Indeed, for long vectors V this might indeed take some time, but if I am =
correct, improvements can be made.
>
> Jos- Hide quoted text -
>
> - Show quoted text -
Dear Ray,
I am sorry I do not have access to the book you just mentioned, so it
is difficult to understand your solution.
Dear Jos,
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.
I am thinking that repmat might be a part of the solution to this,
where you decide the repeating number by the criteria mentioned above
and accordingly repeat the number of elements. any suggestions?
I was also thinking there might be a way I may want to avoid the
appending of arrays, by running time indices parallel to the original
vector.
Say my vector is as above in my original post i.e:
vector =3D [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];
I can then have:
dt =3D 0:0.001:((length(vector)-1)/1000);
So now the number of times it takes for element 1 to satisfy the
random element criteria mentioned in my original post is multiplied
with the relevant time index in dt and so on for the remaining
elements. But I am still left with the problem of calculating the
number of times it loops for each element before it moves to the next
unique element of the vector.
Please kindly help me!!!! :(
adshak
|