I recommend writing down some other cases a bit. So it seems you want to constraint all row-sums and all column-sums:
For N=3, there are 9 vars (i'm assuming a square case here; you did not provide complete info):
x00 x01 x02
x10 x11 x12
x20 x21 x22
Now the constraint matrix looks like:
x00 x01 x02 | x10 x11 x12 | x20 x21 x22
---------------------------------------
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
That's pretty regular. Not it's time to check out matlab's matrix-creation functions. Sadly i'm not much of a matlab-user, but:
the lower half of rows consist of:
- horizontal stacking of N identity-matrices each of size N
the upper half of rows consist of:
- block-diagonal matrix of N 1-row-vectors each of size N
the final matrix is a vertical stacking of both components
A full sparse-matrix python-example (sorry, no matlab here; but there should be nearly a 1:1 mapping), to be more clear would look like:
import numpy as np
import scipy.sparse as sp
N = 3
component_a = sp.hstack([sp.eye(N) for i in range(N)])
row_full_1 = sp.csr_matrix(np.ones(N))
component_b = sp.block_diag([row_full_1 for i in range(N)]) # matlab: blkdiag?
matrix = sp.vstack((component_b, component_a))
print(matrix.todense())
Output:
[[ 1. 1. 1. 0. 0. 0. 0. 0. 0.]
[ 0. 0. 0. 1. 1. 1. 0. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 1. 1. 1.]
[ 1. 0. 0. 1. 0. 0. 1. 0. 0.]
[ 0. 1. 0. 0. 1. 0. 0. 1. 0.]
[ 0. 0. 1. 0. 0. 1. 0. 0. 1.]]
Remark: depending on N, you need to think about using dense or sparse-matrices. Given N, the ratio of non-zeros in the matrix will be 1/N.