Generate efficiently an array of all combinations of a range of elements

1 回表示 (過去 30 日間)
roger why
roger why 2021 年 3 月 17 日
回答済み: Mohammad Sami 2021 年 3 月 18 日
I would like generate efficiently an array of all combinations of a range of integeers between 0 and 3. SO that each element of my array will be in the form of [i j k l] where i, j, k and l are from 0 to 3. I want to use build in Matlab functions Instead of looping like the following:
for i=0:3
for j=0:3
for k=0:3
for l=0:3
possibility=[i j k l];
end
end
end
end
  3 件のコメント
roger why
roger why 2021 年 3 月 17 日
the number of all combinations are 256 which is 4 to the power of 4. perms() gives only 24 of them.

サインインしてコメントする。

採用された回答

Mohammad Sami
Mohammad Sami 2021 年 3 月 18 日
You can try the following.
[x1,x2,x3,x4] = ndgrid(0:3,0:3,0:3,0:3);
allpossible = [x1(:) x2(:) x3(:) x4(:)];

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMultidimensional Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by