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

Answers (2)

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 Comments

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?
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

Sign in to comment.

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.

Categories

Find more on Holidays / Seasons in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!