如何在不重复的情况下获取数组元素的所有可能组合
我有字符串元素的向量vector = {'a','b','c'}
。
我想生成数组的三个元素的所有可能组合,但没有重复。
我期望以下结果:{'a','b','c','ab','ac','bc','abc'}
。
我该如何在MATLAB中这样做?
I have a vector of string elements vector = {'A','B','C'}
.
I want to generate all possible combination of the three elements of the array but without duplicate.
I expect the following result: {'A', 'B', 'C', 'AB', 'AC', 'BC', 'ABC'}
.
How can I do that in MATLAB?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从所需的结果来看,您希望使用
'a'''''
的2个选择的所有组合代码>''(没有)。您可以使用
遵循nchoosek
输出
然后删除空间并将组合转换为单元格数组:
正如沃尔夫(Wolfie)指出的那样,这仅适用于单个字符输入,对于多字符输入,我们可以使用字符串数组而不是char数组:
Judging from your desired result, you want all the combinations with 2 choices of
'A'
,'B'
,'C'
, and''
(nothing). You can do it withnchoosek
as followsOutput
Then removing the spaces and converting the combinations to a cell array:
As Wolfie pointed out, this only works for single character input, for multi character inputs we can use string arrays instead of char arrays: