help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Filling a column with data from a random generator without using a f


From: Michael Creel
Subject: Re: Filling a column with data from a random generator without using a for loop
Date: Fri, 16 Apr 2004 10:10:15 +0200
User-agent: KMail/1.6.1

If you want zeros and ones, you're talking about Bernoulli trials (a binomial 
r.v. with n=1). An easy way to do this is

p = 0.5; # or whatever you like
r = 10; # or whatever
c = 3; # ditto
a = rand(r, c);
a = a < p;

With this, the probability that any element of a is 1 is p, and they're all 
independent. You can get draws from a binomial(c,p) by summing the rows of a.
I suppose that binomial_rnd does more or less the same thing.

Michael

On Friday 16 April 2004 00:09, Henry F. Mollet wrote:
> I need to fill a column in a matrix with zeros and ones according to a
> binomial random generator and don't want to use a for loop. How is it done?
> Henry
>
> matrix(:,1) = binomial_rnd(1,0.5); % using p = 0.5 here, so mean of cols
> should be about 0.5. This produces all zeros or all ones as apparently
> binomial_rnd(1,0.5) is evaluated only once.
> I did this for 5 cols of length 100 and got
> octave:117> mean (matrix)
> ans =
>   1  0  0  1  1
>
> If I use a for loop I get what I want but how do I do it without the for
> loop?
>
> for k=1:100  %for loop for each col
> matrix(k,1) = binomial_rnd(1,0.5);
> endfor
> octave:140> mean(matrix)
> ans =
>   0.49000  0.52000  0.57000  0.47000  0.54000
>
>
>
> -------------------------------------------------------------
> Octave is freely available under the terms of the GNU GPL.
>
> Octave's home on the web:  http://www.octave.org
> How to fund new projects:  http://www.octave.org/funding.html
> Subscription information:  http://www.octave.org/archive.html
> -------------------------------------------------------------



-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------



reply via email to

[Prev in Thread] Current Thread [Next in Thread]