matlab - Why this thing happens with random matrix such that all rows sum up to 1? -
matlab - Why this thing happens with random matrix such that all rows sum up to 1? -
i generate random matrix rows sum one. found question , answers how create random matrix such rows sum 1 want.
the problem when did it, sum not one.
mat = rand(2, 2); rowsum = sum(mat, 2); mat = bsxfun(@rdivide, mat, rowsum); sometimes this:
sum_test = sum(mat, 2)-1 1.0e-15 * 0 -0.2220; i not know why?
matlab uses double-precision numbers, means there finite set of numbers can represented. gap between double-precision numbers can found in matlab using eps function. value of 1, pretty much value when sum each row, eps(1) = 2.220446049250313e-16, same magnitude numerical error you're getting. error introduced when split each value in matrix. more info matlab precision here.
even doing subtracting error first value in each row doesn't solve problem (although help). 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 error first column in each row sum_test2 = sum(mat,2)-1 %still error results in
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 (sum(mat,2)-0.5)-0.5 gives different reply sum(mat,2)-1)
matlab precision
Comments
Post a Comment