Filter löschen
Filter löschen

選択肢から重複を許し​て並べる順列のパター​ンを列挙した行列を作​る方法

39 Ansichten (letzte 30 Tage)
Yuki Koyama
Yuki Koyama am 10 Mai 2020
Kommentiert: Akira Agata am 24 Jun. 2024 um 3:08
たとえば,[0 1]から重複を許して3つ選び,それらを並べるパターンは
[0 0 0],[0 0 1],[0 1 1],...のように列挙できますが,
[[0 0 0];[0 0 1];[0 1 1];...]
のような行列として作りたい場合,for文を用いずに作る方法はありますか?

Akzeptierte Antwort

Akira Agata
Akira Agata am 10 Mai 2020
meshgrid ndgrid 関数を利用する方法では如何でしょうか?
たとえば [0 1] から重複を許して3つ選ぶという例ですと、以下のようになります。
[x1,x2,x3] = meshgrid([0 1],[0 1],[0 1]);
A = [x1(:),x2(:),x3(:)];
>> A
A =
0 0 0
0 1 0
1 0 0
1 1 0
0 0 1
0 1 1
1 0 1
1 1 1
  4 Kommentare
michio
michio am 22 Jun. 2024
R2023b からは combinations 関数もおすすめです。
Akira Agata
Akira Agata am 24 Jun. 2024 um 3:08
ご参考までに。。。
T = combinations([0 1], [0 1], [0 1]);
A = table2array(T) % A = T{:,:} でも可
A = 8x3
0 0 0 0 0 1 0 1 0 0 1 1 1 0 0 1 0 1 1 1 0 1 1 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Resizing and Reshaping Matrices finden Sie in Help Center und File Exchange

Produkte


Version

R2020a

Community Treasure Hunt

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

Start Hunting!