How to make sure each column in a matrix sums exactly to 1?

4 ビュー (過去 30 日間)
Marco
Marco 2015 年 8 月 7 日
コメント済み: Marco 2015 年 8 月 7 日
Hello,
I would need to create a matrix of random numbers between 1 and 0, where the diagonal of the matrix is composed of zeros only and where each column sums to 1. I approximately reached the result by the following code, but some of the columns add up to slightly less than 1 (around 0.97). Why doesn't Matlab manage to normalize all of them to 1? Is there a way to solve the problem?
Here's the code:
a = [0:0.0001:1];
b(1:10000) = (0.5/10000);
F=randsrc(100,100,[a;0.5 b]);
columnSums = sum(F);
N=100;
for n=1:N
F(n,n)=0;
end
for i = 1:numel(columnSums)
F(:,i) = F(:,i)./columnSums(i);
end
Thank you! Marco

採用された回答

Lukas Bystricky
Lukas Bystricky 2015 年 8 月 7 日
It's because you're calculating the column sums and then setting the diagonal entries to 0. If you calculate the sums after you modify the diagonal then what you wrote would work.

その他の回答 (1 件)

Torsten
Torsten 2015 年 8 月 7 日
a = [0:0.0001:1];
b(1:10000) = (0.5/10000);
F=randsrc(100,100,[a;0.5 b]);
N=100;
for n=1:N
F(n,n)=0;
end
columnSums = sum(F);
for i = 1:numel(columnSums)
F(:,i) = F(:,i)./columnSums(i);
end
Best wishes
Torsten.

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by