How to generate all combinations of a vector
1 次查看(过去 30 天)
显示 更早的评论
I want to generate all possible combinations of a vector A=[1 2 3] to get answer [1 1 1; 1 1 2; 1 1 3; 1 2 1; 1 2 2; 1 2 3]
1 个评论
John D'Errico
2018-3-23
You have listed [1 1 1] in the list of "combinations". That would seem to imply that replicates are allowed, since 1 appears only once in A. So, why not [2 2 2] in that set? Why not [3 3 3]? Why not [1 3 3]?
Most of the set you generated are sorted. But you list both [1 1 2], and [1 2 1]. So why not [2 1 1]? Why not [3 2 1]?
If you want a serious answer, you need to explain what you need. Or perhaps give a far better example.
回答(1 个)
David Fletcher
2018-3-23
Generally, perms gives the permutations of a vector eg.
>> perms([1 2 3])
ans =
3 2 1
3 1 2
2 3 1
2 1 3
1 3 2
1 2 3
However, I'm not sure that's what you are asking since you've included [1 1 1] as a possible combination of [1 2 3] - I have no idea how [1 1 1] is a combination of [1 2 3] (though by extension of that logic, I'm not sure why you don't include [2 2 2] and [3 3 3] in your list, as well as numerous other things if you're going to repeat elements)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!