Azzera filtri
Azzera filtri

How to generate all combinations of a vector

2 visualizzazioni (ultimi 30 giorni)
Vishal Sharma
Vishal Sharma il 23 Mar 2018
Commentato: John D'Errico il 23 Mar 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 Commento
John D'Errico
John D'Errico il 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.

Accedi per commentare.

Risposte (1)

David Fletcher
David Fletcher il 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)

Categorie

Scopri di più su Startup and Shutdown in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by