|
On Jul 14, 12:48 pm, "samira " <samira.rash...@gmail.com> wrote:
> Hi all
>
> I know how to make a random vector of a specified size from a set of numbers. Now can you help me how to make a weighted random vector.
> I mean I have this vector [1 2 3], now I want to make another vector of size "n" which randomly includes these numbers. The point is I want to specify that in my random vector, I want 20% of the numbers be 1, 40% be 2 and next 40% be 3.
>
> Is it possible?
>
> Thanks
Hi Samira,
It is possible but the solution depends on whether your random vector
must have on average 20% No 1, 40% No. 2, and 40% No. 3 or the random
vector must have exactly 20% No. 2, 40% No. 2, and 40% No. 3. In the
first case (percents are averages) this might work
t = rand(1, n); % t has random numbers in (0, 1)
v(t<=0.2) = 1; % On average 20% of the numbers will be in (0, 0.2]
v(0.2 < t & t <= 0.6) = 2; % On average 40% of the numbers will be in
(0.2, 0.6]
v(t > 0.6) = 1; % On average 40% of the numbers will be in (0.6, 1)
In the second case (percents are the exact number of 1s, 2s and 3s)
this might work
v = [ones(1, round(0.2*n)), 2*ones(1, 0.4*n), 3*ones(1,
round(0.4*n) )];
Thus far v has the correct percentages but is not random. To make it
random
v = v(randperm(n))
HTH,
Jomar
|