How to create a matrix such that all the row elements add up to 1 and the column elements add up to 1?
古いコメントを表示
Hi,
I'd like to create a NXN matrix, such that each of the row elements add up to 1 and each of the column elements add up to 1 as well. I'd like to create that matrix by specifying a vector that has N elements. For instance if I want to create a 5X5 matrix, I'd like to specify the elements {.4,.2,.1,.1,.2} and arrange them in a 5X5 matrix with each row and each column summing up to 1. I know MATLAB has a "magic" matrix command, but that also imposes an additional constraint that the diagonal elements add up to 1. I want to get rid of that constraint, as it only gives 1 unique matrix per N.
I'd appreciate any help regarding this problem. Thanks, Lakshmi
採用された回答
その他の回答 (2 件)
Sean de Wolski
2012 年 8 月 14 日
編集済み: Sean de Wolski
2012 年 8 月 14 日
Your comment on the constraint making only one unique matrix is not true.
If:
x = magic(5);
then
x'
flipud(x)
fliplr(x)
circshift(x,[1 2])
Are all different and all have columns and rows that sum to the same values. I would take this, above information, do something with it to make MATLAB's magic square randomized and then use it to index into x.
x = [.4,.2,.1,.1,.2]; %x
n = numel(x); %how many x?
p = x(circshift(mod(magic(n),n)+1,randperm(n,2))); %a random selection
Check:
sum(p)
sum(p,2)
カテゴリ
ヘルプ センター および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!