Clear Filters
Clear Filters

How to generate all combinations of a vector

2 views (last 30 days)
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 Comment
John D'Errico
John D'Errico on 23 Mar 2018
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.

Sign in to comment.

Answers (1)

David Fletcher
David Fletcher on 23 Mar 2018
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)

Categories

Find more on Startup and Shutdown 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!