MATLAB: If I have a vector [1 2 3] and I want all combinations of these values along 6 elements so I will have (3^6) combinations in one vector, How to do

comb

If I have a vector [1 2 3] and I want all combinations of these values along 6 elements so I will have (3^6) combinations in one vector, How can I do?

Best Answer

Most likely there's already an implementation on the FileExchange. Otherwise, it's easy to implement:
elements = [1 2 3];
count = 6;
combs = cell(1, count);
[combs{:}] = ndgrid(elements);
combs = reshape(cat(count + 1, combs{:}), [], count)