How to generate all combinations of a vector

1 Ansicht (letzte 30 Tage)
Vishal Sharma
Vishal Sharma am 23 Mär. 2018
Kommentiert: John D'Errico am 23 Mär. 2018
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 Kommentar
John D'Errico
John D'Errico am 23 Mär. 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.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

David Fletcher
David Fletcher am 23 Mär. 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)

Kategorien

Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by