I am trying to get all possible combinations of elements from an array of vectors. For example, say, I have a cell array
C = {[1 2 3 4 5], [6:13], [14 15]}
then the output should be something like
out = {[1 6 14], [1 6 15], [1 7 14], [1 7 15],....,[5 13 15]}
I tried to use a recursive function to implement this, but the code below doesn't seem to work. How can I obtain the list of all these combinations?
function [out,i] = permuteTest2(a,i,N,l,out)
if nargin == 0
a={[1 2],[4 5],[7 8]};
N = length(a);
i = 1;
out = [];
end
if i == N
out = [out, a{i}(l)];
return;
else
for k=i:N
L = length(a{k});
for l=1:L
out =[out a{k}(l)];
[out,i] =permuteTest2(a, i+1, N,l,out);
end
end
end
for ii = 1:length(C); out(ii) = C{ii}(randi(numel(C{ii})));end, i.e. for each element inC, pick a random index and store the number on that index.out, or do you want the complete set of permutations?