How can I create a set from elements and combined elements?
이전 댓글 표시
I have the following vector:
A = [1 2 3 4];
And all it's possible combinations, which I obtained by using the following code
n = length(A);
combinations = {};
counter = 1;
for i = 1:n
CCC = nchoosek(gen,i);
for j = 1 : size(CCC,1)
combinations{counter,1} = CCC(j,:);
counter = counter + 1;
end
end
Then, I obtained the following result:

Now, I need to create something like this:

Note that elements don't repeat
답변 (2개)
Bruno Luong
2020년 12월 8일
편집: Bruno Luong
2020년 12월 8일
Not sure exactly what kind of "combination" you look for, but it looks like a subset of these
b=logical(dec2bin(1:2^4-1,4)-'0');
% https://www.mathworks.com/matlabcentral/fileexchange/24133-set-partition
c=arrayfun(@(r) SetPartition(num2cell(find(b(r,:)))),1:size(b,1),'unif',0);
c=cat(1,c{:});
DispPartObj(c)
Gives this (notice 51 combinations here)
The 51 partition(s) are:
{4}
{3}
{3 4}
{3} {4}
{2}
{2 4}
{2} {4}
{2 3}
{2} {3}
{2 3 4}
{2 3} {4}
{2 4} {3}
{2} {3 4}
{2} {3} {4}
{1}
{1 4}
{1} {4}
{1 3}
{1} {3}
{1 3 4}
{1 3} {4}
{1 4} {3}
{1} {3 4}
{1} {3} {4}
{1 2}
{1} {2}
{1 2 4}
{1 2} {4}
{1 4} {2}
{1} {2 4}
{1} {2} {4}
{1 2 3}
{1 2} {3}
{1 3} {2}
{1} {2 3}
{1} {2} {3}
{1 2 3 4}
{1 2 3} {4}
{1 2 4} {3}
{1 2} {3 4}
{1 2} {3} {4}
{1 3 4} {2}
{1 3} {2 4}
{1 3} {2} {4}
{1 4} {2 3}
{1} {2 3 4}
{1} {2 3} {4}
{1 4} {2} {3}
{1} {2 4} {3}
{1} {2} {3 4}
{1} {2} {3} {4}
댓글 수: 3
Bruno Luong
2020년 12월 8일
EDIT bug on the answer
Franklin Fabián Lucero Luna
2020년 12월 12일
Bruno Luong
2020년 12월 12일
편집: Bruno Luong
2020년 12월 12일
Can you try this?
b=logical(dec2bin(1:2^4-1,4)-'0');
% https://www.mathworks.com/matlabcentral/fileexchange/24133-set-partition
c=arrayfun(@(r) SetPartition(num2cell(find(b(r,:)))),1:size(b,1),'unif',0);
c=cat(1,c{:});
c=cellfun(@(c) cellfun(@(c) [c{:}], c, 'unif', 0), c, 'unif', 0);
DispPartObj(c)
Or use the bug-fix version of DispPartObj attached here
KSSV
2020년 12월 8일
You can initialize a cell and get what you want.
combination = cell(10,1) ;
for i = 1:10
combination{i} = ['combination',num2str(i)] ;
end
combination
Like wise crate other cell with the name ExpectedResult and save the required data.
At the end, the generated cells can be converted to table as well.
카테고리
도움말 센터 및 File Exchange에서 Holidays / Seasons에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
