I want to generate a list, named listt, that contains all possible positive integers that sum into a specific number d. For example, I am looking at the list of all the possible positive integer values for x's such that x1 + x2 + ... + xk = d.
In this case, the row size of listt should be (d+k-1) choose (k-1).
I already have the MATLAB code for doing that (see below), but the main problem is that this code allows repetitive rows in listt. I need help in order to change this code suth that there are no repetitive rows in listt.
k = 2;
d = 2;
m = nchoosek(d+k-1, k-1);
% generate non-negative integer random (m x k) array row-sum to d
ss=rand(m,d+k-1);
[~,r] = maxk(ss,k-1,2);
z = zeros(m,1);
listt = diff([z, sort(r,2), (d+k)+z],1,2)-1;
Let's say d=5 and k=2.
Assume I get the following listt:
1 4
2 3
0 5
5 0
5 0
0 5
So as you can see, the row (5,0) and row (0,5) are repeated two times each. I want to get unique rows.
More precisely, for d=5 and k=2, what do I want to get exactly is the following all 6 possibilities:
1 4
2 3
0 5
5 0
4 1
3 2
Any help will be very appreciated!
Let
ddenote the desired sum andkthe number of components.The code works by representing each solution as a row vector containing
k+1increasing integers starting at1and ending atd+k+1(matrixtin the code). Thek-1inner integers are generated usingnchoosek. The actual solution is then obtained from thekconsecutive differences between those integers.For example, the matrix
tfor inputsd=5andk=2iswhich gives the result