How can I create a set from elements and combined elements?
조회 수: 4 (최근 30일)
이전 댓글 표시
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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/454638/image.jpeg)
Now, I need to create something like this:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/454673/image.jpeg)
Note that elements don't repeat
댓글 수: 0
답변 (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월 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.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Stability Analysis에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!