title: Combination of possible values in an array with multiple constraints in Matlab?
body: <p>I have an array B of :</p>
  
  <pre><code>B=[1 2 3; 10 20 30 ; 100 200 300 ; 1000 2000 3000]
  </code></pre>
  
  <p>such that </p>
  
  <pre><code>B =
  
             1           2           3
            10          20          30
           100         200         300
          1000        2000        3000
  </code></pre>
  
  <p>I am using the following code to find the possible combination between these variables that are below a certain value (a constraint) -- 2000 in this case :</p>
  
  <pre><code>A=[123; 323; 12 ; 421]
  SA=sum(A)
  V=cell(length(B),1);
  n=1;
  for k = 1:length(B)
      for idx = nchoosek(1:length(B), k)'
          B_subset = B(idx);
          if (SA + sum(B_subset) &lt;= 2000)
              V(n)={B_subset(:)}; %store them in a cell
              n=n+1;
          end
      end
  end
  </code></pre>
  
  <p>However I failed to combine them the way I want below.</p>
  
  <hr>
  
  <p>Objective : </p>
  
  <p>Find the possible combinations from B to be added with SA so that their sum is less than 2000 ?</p>
  
  <hr>
  
  <p>Constraint 1 :</p>
  
  <ul>
  <li>Only one value from each row in array <code>B</code> can be used at once.  </li>
  </ul>
  
  <p>For example, this is <strong>NOT</strong> acceptable : [1 2 20] [2 20 30]<br>
  This is the <strong>correct</strong> one : [1 20 100] [3 200 3000]</p>
  
  <hr>
  
  <p>Constraint 2 :
   - The answers should be stored in the cell V in only one column (as initialised in the code above).</p>
  
  <p>The cell should have an output similar to the one I'm currently having :</p>
  
  <pre><code>V = 
  
      [       100]
      [       300]
      [       200]
      [2x1 double]
      [2x1 double]
      [2x1 double]
      [3x1 double]
  </code></pre>