How to create a random matrix so that all rows add up to 1

Here's what the matrix looks like:

There are 8 columns and they say 100 lines, random numbers in any sum of lines equal 1.

.125.125.125.125 ........ 125

.005.105.005.205 ........ 205

.002.003.012. 201 ........ 200

...

Can Matlab automatically create such a matrix, i.e. right stochastic matrix ? I am looking for a script.

+6
source share
5 answers

Use bsxfun , not repmat :

 mat = rand(100, 8); rowsum = sum(mat,2); mat = bsxfun(@rdivide, mat, rowsum); 
+10
source

Here's another idea: for each line, you could generate 7 random numbers (from 0 to 1) and treat them like your "interval" locations - in other words, in your 8 random numbers, the sum of which is 1, these are your partial amount. Then you can sort them and accept the difference to get the random numbers obtained. Here is the code for what I think:

 numrows = 100; partialsums = [zeros(numrows,1), rand(numrows,7), ones(numrows,1)]; partialsums = sort(partialsums, 2); randmat = diff(partialsums, 1, 2); 

The distribution of numbers will vary depending on how you do it. I compared this method with the one published by Aabaz, and I got it for distribution.

enter image description here

So my view looks a little more exponential, you get some higher values, and its a bit more uniform, but with a lower clipping of the random numbers you get.

+6
source

First you can create your random matrix and then normalize it so that each row has a sum equal to 1 (if that's what you had in mind):

 mat=rand(100,8); matnorm=repmat(sum(mat,2),1,8); mat=mat./matnorm; 
+4
source

Step 1: Calculate the norm of the vector, which is the square root of the sum of the squared elements in this vector.

Step 2: divide each element of the vector by the norm of the vector.

Step 3: Multiply the resulting vector by transposing it by the square of each element in the vector.

Then you can see that the sum of the (final) resulting elements in the vector adds up to 1.

+1
source

Well

 rand(100,8) 

will generate a random number matrix with 100 rows and 8 columns, all entries of which can be added to 1. If this does not do what you want, you will have to explain yourself a bit.

-2
source

Source: https://habr.com/ru/post/908620/


All Articles