how can i get all subsets from a matrix
Afficher commentaires plus anciens
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?
Réponses (1)
Guillaume
le 10 Déc 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{:});
Catégories
En savoir plus sur Linear Regression dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!