Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a random matrix such that all rows sum to 1

Tags:

matrix

matlab

Here is what the matrix would look like:

There are 8 columns and say 100 rows, the random numbers in any row sum to 1.

.125 .125 .125 .125 ....... .125

.005 .105 .005 .205 ....... .205

.002 .003 .012. 201 ....... .200

...

Could Matlab automatically creates this kind of matrix, i.e. a right stochastic matrix? I am looking for a script.

like image 405
user1205030 Avatar asked Feb 16 '12 14:02

user1205030


1 Answers

Use bsxfun rather than repmat:

mat = rand(100, 8);
rowsum = sum(mat,2);
mat = bsxfun(@rdivide, mat, rowsum);
like image 134
Nzbuu Avatar answered Sep 27 '22 19:09

Nzbuu