フィルターのクリア

How to generate all combinations of a vector

2 ビュー (過去 30 日間)
Vishal Sharma
Vishal Sharma 2018 年 3 月 23 日
コメント済み: John D'Errico 2018 年 3 月 23 日
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
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
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)

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by