how can i get all subsets from a matrix
1 view (last 30 days)
Show older comments
i have a matrix for example x=
[1 2 3 6 7;4 5 6 8 9;8 7 6 3 1;5 6 7 9 1;6 4 2 9 6]
i want ta take first colomn with the scond and make it matrix from order 5×2 then col:1,3 and col:1,4, col:1,5 then 3 columns 1,2,3 and 1 2 4 and 1 2 4 etc.. these submatcese are 2^5= 32 matrix any help?
0 Comments
Answers (1)
Guillaume
on 10 Dec 2014
m = [1 2 3 6 7;4 5 6 8 9;8 7 6 3 1;5 6 7 9 1;6 4 2 9 6];
out = cell(2^size(m, 2), 1);
outrow = 1;
for numcols = 0:size(m, 2)
colcombs = nchoosek(1:size(m, 2), numcols);
for c = 1:size(colcombs, 1)
out{outrow} = m(:, colcombs(c, :));
outrow = outrow + 1;
end
end
Or using cellfun / arrayfun:
m = [1 2 3 6 7;4 5 6 8 9;8 7 6 3 1;5 6 7 9 1;6 4 2 9 6];
out = arrayfun(@(numcols) cellfun(@(colcomb) m(:, colcomb), num2cell(nchoosek(1:size(m, 2), numcols), 2), 'UniformOutput', false), 0:size(m, 2), 'UniformOutput', false);
out = vertcat(out{:});
0 Comments
See Also
Categories
Find more on Regression in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!