How can I create a set from elements and combined elements?
7 次查看(过去 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:
Now, I need to create something like this:
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 中查找有关 Multidimensional Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!