How i can create random array with fixed sum for each row and column?

3 ビュー (過去 30 日間)
reshdev
reshdev 2014 年 8 月 19 日
編集済み: reshdev 2014 年 8 月 22 日
a

採用された回答

Roger Stafford
Roger Stafford 2014 年 8 月 20 日
編集済み: Roger Stafford 2014 年 8 月 20 日
I assume by "rounded off" you mean the elements are all to be integers. Do this:
A = zeros(4);
for k = 1:7
p = randperm(4); % <-- This is the source of randomness
for ix = 1:4
A(p(ix),ix) = A(p(ix),ix) + 1;
end
end
A = [A(:,1),zeros(4,1),A(:,2:4)]; % Insert a column of four zeros
A = [A(1:3,:);zeros(1,5);A(4,:)]; % Insert a row of five zeros
A(1,1) = 0; A(3,3) = 0; A(5,5) = 0; % Set non-zero diagonal elements to 0
A should be a randomly created 5 x 5 matrix whose elements are all integers ranging from 0 to 7 in such a way that
1) row 1 and column 1 have equal sums,
2) row 2 has a sum of 7 and column 2 has a sum of 0,
3) row 3 and column 3 have equal sums,
4) row 4 has a sum of 0 and column 4 has a sum of 7,
5) row 5 and column 5 have equal sums,
6) its diagonal elements are all zeros, and
7) all column and row sums are 7 or less,

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGenetic Algorithm についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by