Generating combinations under certain constraints
Show older comments
I have 5 variables x1,x2,x3,x4 and x5. each is a 4x1 column vector and I want to generate a matrix that holds the different possible combinations of these 5 variables such that the sum of their product(x1'*x1 +x2'*x2+ x3'*x3 +x4'*x4+ x5'*x5) is less than 10. the elements of the 5 variables x are non-negative integers.
11 Comments
Matt J
on 8 Mar 2015
It's impossible as currently stated. The minimum value of the sum is 20, attained when all x1(i)...x5(i)=1.
Amira Akra
on 8 Mar 2015
John D'Errico
on 8 Mar 2015
Um, you state in one place that the values can be as small as zero, in another place that they be positive integers. Which is it?
Amira Akra
on 8 Mar 2015
John D'Errico
on 8 Mar 2015
Edited: John D'Errico
on 8 Mar 2015
Matt's question is a valid one though. If x1 is a POSITIVE integer vector of length 4, as is x2, x3, etc., then
x1'*x1 = sum(x1.^2)
has a minimum value of 4.
The sum of 5 such terms has a minimum value of 20.
To quote your own explanation...
"the elements of the 5 variables x are positive integers."
So you need either to explain the problem more clearly, or accept that it cannot be solved as such.
Amira Akra
on 8 Mar 2015
Edited: Amira Akra
on 8 Mar 2015
Image Analyst
on 8 Mar 2015
OK, so zero is allowed. But for a given set of x, (x1'*x1 +x2'*x2+ x3'*x3 +x4'*x4+ x5'*x5) is a single number. So what do you mean by "combinations" ? Do you mean like x1 might be multiplied by x3 sometimes and x2 other times? Also, what is the use case for this? In other words, why do you need it?
Amira Akra
on 8 Mar 2015
John D'Errico
on 8 Mar 2015
Not yet. You need to clarify if order matters. Thus, suppose we find a solution
{x1,x2,x3,x4,x5}
then is the set
{x2,x1,x3,x4,x5}
a different solution?
Amira Akra
on 8 Mar 2015
Edited: Amira Akra
on 8 Mar 2015
Accepted Answer
More Answers (2)
Guillaume
on 8 Mar 2015
Here is one way to try the brute force approach:
allx = {x1, x2, x3, x4, x5);
sumprodofset = @(cx) sum(cellfun(@(x) x' * x, cx));
combinations = num2cell(logical(dec2bin(1:2^numel(allx)-1, numel(allx)) - '0'), 2);
combsum = cellfun(@(c) sumprodofset(allx(c)), combinations);
validcombtf = combinations(combsum < 10)
Not sure what you want as an output, maybe this:
validcombx = cellfun(@(v) cell2mat(allx(v)), validcombtf, 'UniformOutput', false)
Or this:
valicombtf = cell2mat(validcombtf);
1 Comment
Amira Akra
on 8 Mar 2015
John D'Errico
on 8 Mar 2015
Edited: John D'Errico
on 8 Mar 2015
There are a fair number of possible solutions, depending on the answers to my questions. We can view the solution in a simple way, regardless. Your clarification of my questions will impact the possible set of solutions.
View this problem as the sum of squares of 20 non-negative integers, such that the sum is no greater than 10. There are only a very restricted set of possibilities we can have for that solution. We can worry about how those integers will be distributed among the vectors afterwards.
So essentially, this problem reduces initially to the set of partitions of the integers 0:10, as a sum of the integers {0,1,4,9}. I'll write that sum in terms of the multiplicity of each square in the sum, where the total multiplicity is exactly 20. So we can have many solutions. Here are a few:
20*0, 19*0+1, 18*0+2*1, ... , 18*0+1*1+1*9.
It looks like there are 23 such fundamental ways we can write the integers 0:10 as such a sum of squares of exactly 20 integers.
Now, for each of those 23 fundamental partitions, now you need only see how the non-zero elements can be distributed among the 5 distinct vectors, and you are done.
Categories
Find more on Numerical Integration and Differentiation 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!