MATLAB: Create matrices of all possible combinations of vectors

combinationscombsmatrixvector

So I have a set of 12 row vectors (of length 500) containing different parameters about a dataset.
I need to run a function on matrices containing all possible combinations of these vectors, which there are 4095 of (not including an empty matrix).
Is there to loop through all these matrices?
For example, if I had 3 vectors A,B,and C I would want the following 7 matrices:
[A], [A; B], [A; B; C], [B; C], [B], [B; C], [C]

Best Answer

Simply define all of the vectors in one variable, here I called it inp:
inp = {'A','B','C'}; % Put your vectors here.
% Generate all combinations:
out = cell(size(inp));
for k = 1:numel(inp)
out{k} = num2cell(nchoosek(1:numel(inp),k),2);
end
out = vertcat(out{:});
% Use indices to select inputs:
for k = 1:numel(out)
% inp(out{k}) % to get a cell array
horzcat(inp{out{k}}) % to get a list
end
Note that you should not define lots of separate variables and try to loop over them: this would be very slow and buggy code: