You can use a trick to generate all variable-size combinations with nchoosek
: introduce 0
to represent "no number" (assuming your numbers are never 0). That will generate duplicates, so you need to apply unique
to the rows of the resulting matrix. Finally, remove zeros from each row and store the results into a cell array:
v = [1,2,3,4,5,6,7,8]; %// data
n = numel(v);
vz = [zeros(1,n-1) v]; %// add n-1 zeros to represent the absence of a number
r = nchoosek(vz,n); %// generate combinations, one in each row
r = unique(r,'rows'); %// remove duplicate rows
result = arrayfun(@(k) nonzeros(r(k,:)).', 1:size(r,1), 'uni',0); %'// delete 0's