You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
create matrix of integers with specified sum in column and row
2 views (last 30 days)
Show older comments
Zaid Ali Basharat
on 23 Feb 2021
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 Comment
Rik
on 23 Feb 2021
What have you tried?
Also, don't forget that this might not be possible for some values provided by the user.
Answers (1)
Matt J
on 23 Feb 2021
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 Comments
Zaid Ali Basharat
on 23 Feb 2021
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
on 23 Feb 2021
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
on 23 Feb 2021
Edited: Rik
on 23 Feb 2021
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
on 23 Feb 2021
Edited: Matt J
on 23 Feb 2021
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
on 24 Feb 2021
UPDATE: I can have float values as well. Integer is not necessary. Can we have solution without enlarging values?
Zaid Ali Basharat
on 24 Feb 2021
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
on 24 Feb 2021
No. I am getting more zero values and also these are again integers. I mean this is not the optimal solution
Zaid Ali Basharat
on 24 Feb 2021
Edited: Zaid Ali Basharat
on 24 Feb 2021
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
on 24 Feb 2021
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
on 24 Feb 2021
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
on 24 Feb 2021
Edited: Matt J
on 24 Feb 2021
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
on 24 Feb 2021
Ok now I got what I wanted but is there anyway we can do it without exapnding the upper column values?
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- 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)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)