1

I'm trying to build all possible arrays of length n of a vector of n elements with at least 2 integers in each position. I should be getting 2^n combinations, 16 in this case. My code is generating only half of them, and not saving the output to an array

allinputs = {[1 2] [2 3] [3 4] [5 6]}
A = []

the command I run is

inputArray = inputBuilder(A,[],allinputs,1)

for the function

function inputArray = inputBuilder(A,currBuild, allInputs, currIdx)

    if currIdx <= length(allInputs)

        for i = 1:length(allInputs{currIdx})
            mybuild = [currBuild allInputs{currIdx}(i)];
            inputBuilder(A,mybuild,allInputs,currIdx + 1);

        end

        if currIdx == length(allInputs)
            A = [A mybuild];

            %debug output
            mybuild
        end


        if currIdx == 1
            inputArray = A;
        end


    end

end

I want all 16 arrays to get output in a vector. Or some easy way to access them all. How can I do this?

EDIT: Recursion may be a requirement because allinputs will have subarrays of different lengths. allinputs = {[1] [2 3] [3 4] [5 6 7]}

with this array it will be 1*2*2*3 or 12 possible arrays built

Drew
  • 91
  • 10
  • 2
    Why not use existing methods/functions for getting all possible combinations of a vector or cell? Or maybe you want something sophisticated? – Marcin Nov 22 '14 at 02:15
  • For example, use `allcomb.m` function from file exchange and generate all the possible combinations. Index your cell array accordingly. Is using recursion a requirement for you? – Autonomous Nov 22 '14 at 02:20
  • Recursion wasn't a requirement but it's the way I think. Also wanted to learn matlab better but marcin's answer is pretty close. Thanks! – Drew Nov 22 '14 at 04:18
  • Actually recursion might be a requirement because allinput's subarrays won't be of the same length. – Drew Nov 22 '14 at 05:04
  • I don't fully understand what your expected result is. But since you say "16 arrays", it looks like you want one element taken from each input vector. In that case, see [this](http://stackoverflow.com/questions/21895335/generate-a-matrix-containing-all-combinations-of-elements-taken-from-n-vectors) – Luis Mendo Nov 22 '14 at 11:02
  • @LuisMendo I edited it a little. The first array I listed would make 16 possible arrays. The jagged second one at the bottom will make 12. If there were 3 items in each it would make 3^4 arrays, etc. It looks like your linked page will work though – Drew Nov 22 '14 at 14:13
  • @drew Yes, the answers to the question I linked do exactly that. If you confirm that solves your question, let me know so that I can close it as duplicate – Luis Mendo Nov 22 '14 at 16:33
  • @LuisMendo sure, close it as a duplicate. I suppose my issues with recursion is an entirely separate question – Drew Nov 22 '14 at 17:33

1 Answers1

0

Not sure exactly if this is what you want, but one way of doing what I think you want to do is as follows:

  allinputs = {[1 2] [2 3] [3 4] [5 6]};

  comb_results = combn([1 2],4);

  A = zeros(size(comb_results));
  for rowi = 1:size(comb_results, 1)
      indices = comb_results(rowi,:);

      for idxi = 1:numel(indices)
          A(rowi, idxi) = allinputs{idxi}(indices(idxi));
      end

  end

This gives:

A =

     1     2     3     5
     1     2     3     6
     1     2     4     5
     1     2     4     6
     1     3     3     5
     1     3     3     6
     1     3     4     5
     1     3     4     6
     2     2     3     5
     2     2     3     6
     2     2     4     5
     2     2     4     6
     2     3     3     5
     2     3     3     6
     2     3     4     5
     2     3     4     6

combn is here.

Marcin
  • 215,873
  • 14
  • 235
  • 294
  • I'm sure I can figure it out but what if I want each row in its own array? – Drew Nov 22 '14 at 04:23
  • @drew in this case you can make A as a cell array and adjust the code accordingly. – Marcin Nov 22 '14 at 04:28
  • actually it looks like recursion may be a requirement because allinputs won't have subarrays of all the same length. Any idea what fundamental part of matlab recursion it is that I don't understand? – Drew Nov 22 '14 at 05:03