[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
## Filling a column with data from a random generator without using a for l

**From**: |
Henry F. Mollet |

**Subject**: |
Filling a column with data from a random generator without using a for loop |

**Date**: |
Thu, 15 Apr 2004 15:09:04 -0700 |

**User-agent**: |
Microsoft-Entourage/10.1.1.2418 |

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
-------------------------------------------------------------

**Filling a column with data from a random generator without using a for loop**,
*Henry F. Mollet* **<=**