What you have is often called integer-linear programming and it is known to be NP-hard (which means don't hold your breath until a solution comes out).
If you want to solve it without the integerness, you have a linear program and hence can use linprog
. If you think of your unknown matrix as vector of unknown entries then column sum is just
col_sum = kron(eye(4),[1,1,1]);
col_sum =
1 1 1 0 0 0 0 0 0 0 0 0
0 0 0 1 1 1 0 0 0 0 0 0
0 0 0 0 0 0 1 1 1 0 0 0
0 0 0 0 0 0 0 0 0 1 1 1
Similarly, row sum is
row_sum = repmat(eye(3),1,4);
row_sum =
1 0 0 1 0 0 1 0 0 1 0 0
0 1 0 0 1 0 0 1 0 0 1 0
0 0 1 0 0 1 0 0 1 0 0 1
These are your equality constraints also you have the inequality constraints but only to bound the unknown values. linprog
can bound them as extra arguments. However you don't have an objective function which you can make up something like sum of all unknowns minimized or one of them or any other linear objective would do or you can leave it empty and you get any feasible result.
Aeq = [col_sum;row_sum]
beq = [2 6 6 1 5 7 3]';
X = linprog([],[],[],Aeq,beq,zeros(12,1),10*ones(12,1))% 0 <= vars <= 10
X = reshape(X,3,4)
X =
0.6550 2.0160 2.0160 0.3130
1.1192 2.5982 2.5982 0.6845
0.2258 1.3859 1.3859 0.0025
>> sum(X,1)
ans =
2.0000 6.0000 6.0000 1.0000
>> sum(X,2)
ans =
5.0000
7.0000
3.0000
If you have certain entries that are guaranteed to be zero etc. Then it might be possible that all solutions are forced to be integers. Otherwise you need to have nonconvex specific integer programming solvers for example given here