A random symmetric matrix whose row and column sum is equal to one and diagonal is zero

9 ビュー (過去 30 日間)
i want a random symmetric matrix whose row and column sum is equal to one and diagonal is zero. (The application of this matrix is in the weight graph )

採用された回答

John D'Errico
John D'Errico 2022 年 10 月 11 日
編集済み: John D'Errico 2022 年 10 月 11 日
If it is symmetric, AND the row sums are 1, then so are the column sums.
But if you are not picky about the distribution of the elements of the matrix. then it is trivial to do. I'll assume from your comments that you want a n all-nonnegative matrix.
N = 5; % the dimension of the matrix
X = randn(N,2);
M = (X(:,1) - X(:,1).').^2 + (X(:,2) - X(:,2).').^2;
; % M is symmetric, with a zero diagonal. Now scale the matrix.
% using an iterative scheme...
flag = true;
while flag
s = sqrt(1./sum(M,1));
flag = norm(s - ones(1,N)) > 1e-16;
M = s.*M.*s.';
end
M
M = 5×5
0 0.2997 0.0976 0.2464 0.3562 0.2997 0 0.2287 0.2415 0.2300 0.0976 0.2287 0 0.3860 0.2877 0.2464 0.2415 0.3860 0 0.1261 0.3562 0.2300 0.2877 0.1261 0
% row sums
sum(M,2)
ans = 5×1
1.0000 1.0000 1.0000 1.0000 1.0000
% column sums
sum(M,1)
ans = 1×5
1.0000 1.0000 1.0000 1.0000 1.0000
Easy enough. I imagine I could come up with a scheme with minor thought that is not iterative, but the above will be convergent, and I don't see a reason to make something computationally elegant when there is no gain from doing so. Is the distribution of the elements of that matrix anything special? They are certainly not uniform. GOD NO! But then, you never specified a distribution.
  2 件のコメント
mohammad
mohammad 2022 年 10 月 11 日
Thank you very much sir. Your algorithm worked in MATLAB 2021 but gives an error in MATLAB 2014. Can you fix it?
Error using -
Matrix dimensions must agree in line M = (X(:,1) - X(:,1).').^2 + (X(:,2) - X(:,2).').^2;
because size(X(:,1)) is 5*1 but size size(X(:,1).') is 1*5
John D'Errico
John D'Errico 2022 年 10 月 11 日
Just use bsxfun instead. That line of code requires R2016b or later, when the idea of a singleton dimension expansion was introduced. This should work:
M = bsxfun(@minus,X(:,1),X(:,1).').^2 + bsxfun(@minus,X(:,2),X(:,2).').^2;

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

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