MATLAB: Generating combinations under certain constraints

combinationmatrix

I have 5 variables x1,x2,x3,x4 and x5. each is a 4×1 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.

Best Answer

It might be helpful to recognize that the problem is equivalent simply to selecting a sequence of 20 square integers. Because of your constraints, the set of square integers you can choose from are {0,1,4,9}. There are only about 7 ways the non-zero square integers can be distributed disregarding order,
  • 9
  • 9 1
  • 4 4
  • 4 4 1
  • 4 4 1 1
  • One 4 and up to 6 ones
  • Up to 10 ones
It should be a simple matter to use nchoosek() to identify all the possible selections for each case.