How to obtain all permutations of multiple cell arrays?
6 次查看(过去 30 天)
显示 更早的评论
I have a few cell arrays of character vectors - let's say 3 in this case - A, B, C.
A={'a1'; 'a2'; 'a3'};
B={'b1'; 'b2'};
C={'c1'; 'c2'; 'c3'; 'c4'};
I wish to find all permutations of elements of A, B, and C. That is, one element from each. Such as:
{'a1', 'b1', 'c1'; 'a1', 'c1', 'b1'; 'b1', 'a1', 'c1'; 'a1', 'b1', 'c2'; .... }
Please note that I am not looking for only combinations - I believe that would have been easily addressed by:
X=combinations(A, B, C);
I am also not looking for permutations that would produce combinations/permutations of elements from within one of the cell arrays above - so that I couldn't just do:
X=vertcat(A,B,C);
perms(X);
Is there an efficient way to do this without all sorts of loops?
0 个评论
采纳的回答
更多回答(2 个)
Matt J
2024-3-17
编辑:Matt J
2024-3-17
Using this FEX download,
c= table2cell(combinations(C,B,A));
result = blkColon( c(:,perms(1:3)') , [1,3])
result =
144×3 cell array
{'a1'} {'b1'} {'c1'}
{'a2'} {'b1'} {'c1'}
{'a3'} {'b1'} {'c1'}
{'a1'} {'b2'} {'c1'}
{'a2'} {'b2'} {'c1'}
{'a3'} {'b2'} {'c1'}
{'a1'} {'b1'} {'c2'}
{'a2'} {'b1'} {'c2'}
{'a3'} {'b1'} {'c2'}
{'a1'} {'b2'} {'c2'}
{'a2'} {'b2'} {'c2'}
{'a3'} {'b2'} {'c2'}
{'a1'} {'b1'} {'c3'}
...
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!