From: Christiaan on
I would like to create a vector [1x 200] which contains the values 1 and 0. The amount of ones is depending on a certain preset percentage and is randomly distrubuted over this vector. What is the best way to create such a vector?


From: Matt J on
"Christiaan " <chradem(a)hotmail.com> wrote in message <hnt1o9$2vl$1(a)fred.mathworks.com>...
> I would like to create a vector [1x 200] which contains the values 1 and 0. The amount of ones is depending on a certain preset percentage and is randomly distrubuted over this vector. What is the best way to create such a vector?
>
>

I don't see how a "preset" percentage can be randomly distributed over anything, but maybe this is what you want:

rand(1,200)>=percentage/100
From: Matt J on

> I don't see how a "preset" percentage can be randomly distributed over anything, but maybe this is what you want:
>
> rand(1,200)>=percentage/100

Make that

rand(1,200)<=percentage/100; %"percentage" is a fixed percentage of ones
From: Amanda Galtman on

"Christiaan " <chradem(a)hotmail.com> wrote in message
news:hnt1o9$2vl$1(a)fred.mathworks.com...
>I would like to create a vector [1x 200] which contains the values 1 and 0. The
>amount of ones is depending on a certain preset percentage and is randomly
>distrubuted over this vector. What is the best way to create such a vector?
>
>

If you happen to have the Communications Toolbox, check out the RANDERR
function.

randerr(1,200,7) % For example, 7 ones and 193 zeros

http://www.mathworks.com/access/helpdesk/help/toolbox/comm/ref/randerr.html

HTH,
Amanda


From: Oleg Komarov on
A solution:

% Preset percentage
perc = 70;

% Length of the output vector
vLen = 500;

% Preallocate
Out = zeros(vLen,1);

% Create vLen random positions
pos = randperm(vLen);

% Scatter perc*vLen ones in random positions
Out(pos(1:round(vLen*perc/100)),1) = 1;

% Check
sum(Out)/vLen

Oleg
 |  Next  |  Last
Pages: 1 2
Prev: ODE and external function as input
Next: axis units