# combinations and permutations, how can I formulate it?

What mathematical formulation that can help me to generate all possible (N x N) matrices where the sum of all elements inside each matrix equal constant number M ? Note that: M > N If it can be solved using combinations and permutations, how can I formulate it ?

• I assume you're looking for nonnegative integer entries.With the requirements here there's no need to think of matrices. This is the number of partitions of $M$ into $N^2$ parts, allowing $0$'s. I don't think there's an easy way to count those. See en.wikipedia.org/wiki/Partition_(number_theory) – Ethan Bolker Aug 14 '18 at 16:37
• How can I calculate all possible partitions of M into N^2 elements ? – WASEL Team Aug 14 '18 at 16:41
• As I said in my comment, there is no easy way to do that. You can start at the wikipedia page to study the question. If $M$ isn't too large a brute force recursive computer program will do the job. – Ethan Bolker Aug 14 '18 at 16:45
• @EthanBolker: if order matters, and it seems to here, we are looking for compositions. The number of those is easy – Ross Millikan Aug 14 '18 at 18:44

If the positions of the matrix are distinct you are looking for weak compositions of $M$ into $N^2$ pieces. There are ${M+N^2-1 \choose N^2-1}$ of them. The Wikipedia article derives this.
• I didn't mean the numbers in the matrix had to be different, just that you care where the numbers go in the matrix. For example, $\begin {pmatrix}1&2\\3&2 \end {pmatrix}$ is different from $\begin {pmatrix}2&1\\3&2 \end {pmatrix}$ – Ross Millikan Aug 14 '18 at 18:48