Prev: ODE and external function as input
Next: axis units
From: Christiaan on 18 Mar 2010 07:11 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 18 Mar 2010 07:39 "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 18 Mar 2010 07:46 > 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 18 Mar 2010 07:53 "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 18 Mar 2010 08:29
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 |