create matrix of integers with specified sum in column and row
2 次查看(过去 30 天)
显示 更早的评论
Zaid Ali Basharat
2021-2-23
I want to create a 4x4 matrix of integers such that:-
Say, M=[ A B C D ; E F G H ; I J K L ; M N O P ]
A+B+C+D>=value from user
A+B+C+D<=value from user
A+E+I+M>=value from user
A+E+I+M<=value from user
and likewise for all columns and rows.
1 个评论
Rik
2021-2-23
What have you tried?
Also, don't forget that this might not be possible for some values provided by the user.
回答(1 个)
Matt J
2021-2-23
You can try to formulate it as an integer program.
M=optimvar('M',[4,4],'type','integer');
Constraints.columns=sum(M,1)<=colvalues;
Constraints.rows=sum(M,2)<=rowvalues;
prob=optimproblem('Objective', 0,'Constraints',Constraints );
sol=solve(prob)
16 个评论
Zaid Ali Basharat
2021-2-23
I have tried this:-
M=optimvar('M',[4,4],'type','integer');
colvalues=[21 24 34 28];
rowvalues=[7 6 9 11]';
Constraints.columns=sum(M,1)<=colvalues;
Constraints.rows=sum(M,2)<=rowvalues;
prob=optimproblem('Objective', 0,'Constraints',Constraints );
sol=solve(prob)
And got this output:-
>> test
LP: Optimal objective value is 0.000000.
Optimal solution found.
Intlinprog stopped at the root node because the objective value is within a gap tolerance of the optimal value,
options.AbsoluteGapTolerance = 0 (the default value). The intcon variables are integer within tolerance,
options.IntegerTolerance = 1e-05 (the default value).
sol =
struct with fields:
M: [4×4 double]
I could not find the output Matrix. I opened struct M but it is filled with 'inf' values.
Matt J
2021-2-23
You also need lower bounds, don't you?
Constraints.columns=[sum(M,1)<=upper_colvalues; sum(M,1)>=lower_colvalues]
and similarly for the rows.
Zaid Ali Basharat
2021-2-23
编辑:Rik
2021-2-23
clc;
clear;
clear all;
M=optimvar('M',[4,4],'type','integer');
upper_colvalues=[12 12 12 12];
lower_colvalues=[6 6 6 6];
upper_rowvalues=[40 40 40 40]';
lower_rowvalues=[20 20 20 20]';
Constraints.columns=[sum(M,1)<=upper_colvalues; sum(M,1)>=lower_colvalues];
Constraints.rows=[sum(M,2)<=upper_rowvalues; sum(M,2)>=lower_rowvalues];
prob=optimproblem('Objective', 0,'Constraints',Constraints );
sol=solve(prob);
Please check if this is the write code. I have also tried:-
Constraints.columns=sum(M,1)<=upper_colvalues;
Constraints.columns=sum(M,1)>=lower_colvalues;
But then it gives upper_colvalues and lower_colvalues with 'inf' entries.
Matt J
2021-2-23
编辑:Matt J
2021-2-23
Here's what I get,
M=optimvar('M',[4,4],'type','integer');
upper_colvalues=[12 12 12 12];
lower_colvalues=[6 6 6 6];
upper_rowvalues=[40 40 40 40]';
lower_rowvalues=[20 20 20 20]';
Constraints.columnsUB= sum(M,1)<=upper_colvalues;
Constraints.columnsLB= sum(M,1)>=lower_colvalues;
Constraints.rowsUB = sum(M,2)<=upper_rowvalues;
Constraints.rowsLB = sum(M,2)>=lower_rowvalues;
prob=optimproblem('Objective', 0,'Constraints',Constraints );
sol=solve(prob)
Solving problem using intlinprog.
No feasible solution found.
Intlinprog stopped because no point satisfies the constraints.
sol = struct with fields:
M: []
But if we enlarge upper_colvalues,
Constraints.columnsUB = sum(M,1)<=[12 12 12 12]*4;
>> sol.M
ans =
0 0 0 20
0 0 0 20
0 0 20 0
6 6 0 8
Zaid Ali Basharat
2021-2-24
UPDATE: I can have float values as well. Integer is not necessary. Can we have solution without enlarging values?
Zaid Ali Basharat
2021-2-24
I made these changes and got this:-
M=optimvar('M',[4,4],'type','continuous');
upper_colvalues=[12 12 12 12]*4;
>> sol.M
ans =
20 0 0 0
0 0 0 20
0 0 0 20
0 6 6 8
Zaid Ali Basharat
2021-2-24
No. I am getting more zero values and also these are again integers. I mean this is not the optimal solution
Zaid Ali Basharat
2021-2-24
编辑:Zaid Ali Basharat
2021-2-24
Let me restate the question:-
User will input lower and upper bound values for each row and column in a 4x2 matrix each.
I need to create a 4x4 matrix such that the sum of each row is inside the range defined by the user and likewise for each column.
Matt J
2021-2-24
No. I am getting more zero values and also these are again integers. I mean this is not the optimal solution.
Integers are not forbidden and zeros are not forbidden, based on what you've told us. What is sub-optimal about the solution?
Zaid Ali Basharat
2021-2-24
OUTPUT USER INPUTS
2 7 9 6 = 26 20-30
8 9 10 11 = 38 30-40
3 5 1 0 = 9 5-15
4 8 2 4 = 18 15-20
= = = =
17 29 22 21
10-25 8-30 22-40 15-30
This is the kind of output I want for given input ranges.
Matt J
2021-2-24
编辑:Matt J
2021-2-24
Your example doesn't clarify what you see as wrong with the solution given by solve(). Are you saying you don't believe the input ranges were satisfied. That is fairly easy to test.
M=optimvar('M',[4,4]);
upper_colvalues=[12 12 12 12]*4;
lower_colvalues=[6 6 6 6];
upper_rowvalues=[40 40 40 40]';
lower_rowvalues=[20 20 20 20]';
Constraints.columnsUB= sum(M,1)<=upper_colvalues;
Constraints.columnsLB= sum(M,1)>=lower_colvalues;
Constraints.rowsUB = sum(M,2)<=upper_rowvalues;
Constraints.rowsLB = sum(M,2)>=lower_rowvalues;
prob=optimproblem('Objective', 0,'Constraints',Constraints );
sol=solve(prob); sol.M,
Solving problem using linprog.
Optimal solution found.
ans = 4×4
20 0 0 0
0 0 0 20
0 0 0 20
0 6 6 8
The bounds look well-satisfied to me:
column_check =[lower_colvalues;sum(sol.M,1); upper_colvalues],
column_check = 3×4
6 6 6 6
20 6 6 48
48 48 48 48
row_check =[lower_rowvalues,sum(sol.M,2), upper_rowvalues].'
row_check = 3×4
20 20 20 20
20 20 20 20
40 40 40 40
Zaid Ali Basharat
2021-2-24
Ok now I got what I wanted but is there anyway we can do it without exapnding the upper column values?
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)