필터 지우기
필터 지우기

How can I create a set from elements and combined elements?

조회 수: 4 (최근 30일)
Franklin Fabián Lucero Luna
Franklin Fabián Lucero Luna 2020년 12월 8일
편집: Bruno Luong 2020년 12월 12일
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
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
Franklin Fabián Lucero Luna
Franklin Fabián Lucero Luna 2020년 12월 12일
Thanks! It is useful!
When I use the code provded, I obtain the following result:
When I see c variable, I can find all the partitions I need, nevertheless, they are in a sublevel of the {} structure. How did you printed the partitions?
Bruno Luong
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
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.

카테고리

Help CenterFile Exchange에서 Stability Analysis에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by