重複ありの順列のパターンを列挙した行列を作る方法

1,2,3,4の数字を使って作成した8桁の数字の全てのパターンを列挙した行列を作るにはどうしたら良いですか?
例えば、[[1 1 1 1 1 1 1 1];[1 1 1 1 1 1 1 2];[1 2 3 4 1 2 3 4];.....] のように列挙されて、パターンは全部で4^8通りで、4^8行8列の行列が出来上がると思います。

 Respuesta aceptada

Tomohiko
Tomohiko el 7 de Abr. de 2021

0 votos

1ベースの4進数を1から4^8-1まで並べるという考えで、次の方法はどうでしょうか?
double(dec2base(1:(4^8-1),4))-double('0')+1

3 comentarios

Akane Aoki
Akane Aoki el 7 de Abr. de 2021
なるほど!
ありがとうございます!!!
Akira Agata
Akira Agata el 8 de Abr. de 2021
若干泥臭いですが、ndgrid を使うという方法もあります。
[c1,c2,c3,c4,c5,c6,c7,c8] = ndgrid(1:4,1:4,1:4,1:4,1:4,1:4,1:4,1:4);
A = [c1(:),c2(:),c3(:),c4(:),c5(:),c6(:),c7(:),c8(:)];
Akane Aoki
Akane Aoki el 20 de Abr. de 2021
ありがとうございます!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 6 de Abr. de 2021

Comentada:

el 20 de Abr. de 2021

Community Treasure Hunt

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

Start Hunting!