Why does this happen with a random matrix so that all rows add up to 1?

I would like to create a random matrix whose rows add up to one. I found this question and answered How to create a random matrix so that all rows add up to 1 , which does what I exactly want.

The problem is when I did this, sometimes the amount is not exactly one.

mat      = rand(2, 2);
rowsum   = sum(mat, 2);
mat      = bsxfun(@rdivide, mat, rowsum);

      

Sometimes I get something like this:

sum_test = sum(mat, 2)-1
1.0e-15 *
      0
-0.2220;

      

I do not know why?

0


source to share


1 answer


Matlab uses double precision numbers, which means that there is only a finite set of numbers that can be represented. The gap between double precision numbers can be found in Matlab using the function eps

. For a value of 1, which is pretty much the value you get when you sum each row eps(1) = 2.220446049250313e-16

, which is the same numerical numerical error that you get. The error is introduced when you divide each value in the matrix. More information on Matlab precision here .

Even something like subtracting the error from the first value in each line does not completely solve the problem (although it does help). For example,

mat = rand(10,10);
rowsum = sum(mat,2);
mat = bsxfun(@rdivide,mat,rowsum);
sum_test = sum(mat,2)-1    %finite error
mat(:,1) = mat(:,1) - sum_test; %subtract the error from the first column in each row
sum_test2 = sum(mat,2)-1   %still get error

      

leads to



sum_test = [
-1.110223024625157e-16
                     0
                     0
                     0
                     0
-2.220446049250313e-16
                     0
-2.220446049250313e-16
-1.110223024625157e-16
-2.220446049250313e-16]

      

and

sum_test2 = [
 2.220446049250313e-16
                     0
                     0
                     0
                     0
                     0
                     0
                     0
-1.110223024625157e-16
                     0]

      

(You can show that even (sum(mat,2)-0.5)-0.5

gives a different answer than sum(mat,2)-1

)

+2


source







All Articles