MATLAB: Choose elements from array

choose

Hi My question is how to pick up elements from data. if each element can be chosen or not, it will be 2^n. suppose we have 3 elements ,2^3 types can be picked . n element in 2^n.what's the command of this format?

Best Answer

To compute a cellarray 'combinations' with all 2^N possible combinations of elements, you can use:
val = 1:3;
N = numel(val);
binind = dec2bin( [0 1:2^N - 1]);
for i = 1:2^N
combinations{i} = val(logical(binind(i,:) - '0'));
end