フィルターのクリア

Creating two random matrices

7 ビュー (過去 30 日間)
loukil sana
loukil sana 2016 年 11 月 8 日
編集済み: James Tursa 2016 年 11 月 8 日
Hi. I have 2 matrices. M1(4,6) and M2(4,2). The generation of the two matrices is random. Attached you can find an explanation of the constraints. How can I formulate that with an ILP? Thanks
  4 件のコメント
James Tursa
James Tursa 2016 年 11 月 8 日
Does this mean the individual entries in M1 and M2 need to be integers?
loukil sana
loukil sana 2016 年 11 月 8 日
yes. https://www.mathworks.com/videos/mixed-integer-linear-programming-in-matlab-91541.html

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

採用された回答

James Tursa
James Tursa 2016 年 11 月 8 日
編集済み: James Tursa 2016 年 11 月 8 日
E.g.,
% random matrix
M = rand(4,8);
% normalize per the 4-element sums
M(:,1:4) = bsxfun(@rdivide,M(:,1:4),sum(M(:,1:4),2));
M(:,5:8) = bsxfun(@rdivide,M(:,5:8),sum(M(:,5:8),2));
% scale per the desired sum
M(1:2,1:4) = M(1:2,1:4) * 0.50 * 2000;
M(1:2,5:8) = M(1:2,5:8) * 0.50 * 2000;
M(3:4,1:4) = M(3:4,1:4) * 0.70 * 2000;
M(3:4,5:8) = M(3:4,5:8) * 0.30 * 2000;
% divy up the results
M1 = M(:,[1:3 5:7]);
M2 = M(:,[4 8]);
EDIT: For required integer entries, you can use this ad-hoc modification (not quite exactly uniform since the fractional parts always go into M2, but maybe close enough for your purposes)
% random matrix
M = rand(4,8);
% normalize per the 4-element sums
M(:,1:4) = bsxfun(@rdivide,M(:,1:4),sum(M(:,1:4),2));
M(:,5:8) = bsxfun(@rdivide,M(:,5:8),sum(M(:,5:8),2));
% scale per the desired sum
M(1:2,1:4) = M(1:2,1:4) * 0.50 * 2000;
M(1:2,5:8) = M(1:2,5:8) * 0.50 * 2000;
M(3:4,1:4) = M(3:4,1:4) * 0.70 * 2000;
M(3:4,5:8) = M(3:4,5:8) * 0.30 * 2000;
% Adjust values to integers
F = floor(M);
D = M - F;
F(:,4) = F(:,4) + round(sum(D(:,1:4),2));
F(:,8) = F(:,8) + round(sum(D(:,5:8),2));
% divy up the results
M1 = F(:,[1:3 5:7]);
M2 = F(:,[4 8]);

その他の回答 (1 件)

Image Analyst
Image Analyst 2016 年 11 月 8 日

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by