how can i get all subsets from a matrix
조회 수: 2 (최근 30일)
이전 댓글 표시
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
답변 (1개)
Guillaume
2014년 12월 10일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!