How can i get all the possible combinations of elements of a matrix?i dont want elements of the same column.i want to create vectors whose sum is smaller than a number
2 views (last 30 days)
Show older comments
i dont want elements of the same column. i want to create vectors whose sum is smaller than a number and put them in a new matrix
4 Comments
Answers (2)
Roger Stafford
on 13 Feb 2014
I had put your problem aside, Nikos, and only got around to working it out today. I have taken one liberty with your request. In the G matrix of your last example the first element was always set to a fixed zero value which seems rather useless to bother with, so in this routine that first element is allowed to increase above zero. Therefore the rows of G will contain all possible sequences of n elements in which each element is a non-negative integer greater than the previous element and for which their entire sum is less than the number N. The rows of G will be in lexicographical order. You may enter any value of n you wish (provided there is enough memory to hold the resulting G.) You will note that this routine appears to repeat itself once. That is because I could discover no easy formula for the required number of rows in G in terms of N and n, so the first pass simply makes a count of the necessary number the brute force way, and then allocates the proper amount of memory space for G. On the second pass it proceeds to fill G. This is admittedly rather repetitious, but it beats trying to concatenate rows onto an unallocated G one-row-at-a-time, which could slow you down rather seriously for large values of N and n.
N = 33; % Set N and n to your desired values
n = 5;
N2 = N - n*(n-1)/2;
for p = 1:2
a = -ones(1,n);
s = -ones(1,n);
k = 1;
j = 0;
while k >= 1
if s(k)+1+(a(k)+1)*(n-k) < N2
a(k:n) = repmat(a(k)+1,1,n-k+1);
s(k:n) = cumsum([s(k)+1,a(k+1:n)]);
k = n;
j = j + 1;
if p == 2 % Wait for the second pass to fill G
G(j,:) = a;
end
else
k = k - 1;
end
end
if p == 1 % Allocate space for G at the end of the first pass
G = zeros(j,n);
end
end
G = G + repmat(0:n-1,size(G,1),1);
3 Comments
Roger Stafford
on 14 Feb 2014
The 'combnk' function does not produce the same result as the one you described, Nikos, and the one I have coded for you. In particular it does not give all possible combinations whose sum is less than a number N. I suggest you try and compare them to see for yourself their differences.
Roger Stafford
on 14 Feb 2014
Nikos, there is a better way of determining the number of rows in G. It is not a formula but involves an iteration that is much quicker that making two passes as the other method does. Here is the complete code:
N = 82; % Choose the desired N. It must be greater than n*(n-1)/2
n = 12; % Choose n
N2 = N - n*(n-1)/2;
C = zeros(N2,n); % Set up a table for computing the number of combinations
C(:,1) = (1:N2)';
for k = 2:n
for q = 1:N2
C(q,k) = sum(C(q:-k:1,k-1));
end
end % C(N2,n) has the number of combinations at exit here
G = zeros(C(N2,n),n); % Now we can allocate memory for G
a = -ones(1,n);
s = -ones(1,n);
k = 1;
j = 0;
while k >= 1
if s(k)+1+(a(k)+1)*(n-k) < N2
a(k:n) = repmat(a(k)+1,1,n-k+1);
s(k:n) = cumsum([s(k)+1,a(k+1:n)]);
k = n;
j = j + 1;
G(j,:) = a;
else
k = k - 1;
end
end
G = G + repmat(0:n-1,size(G,1),1);
0 Comments
See Also
Categories
Find more on Loops and Conditional Statements 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!