Creating two random matrices

조회 수: 1 (최근 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 CenterFile Exchange에서 Random Number Generation에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by