Generate a N length vector from a M length list (permutation)

1 回表示 (過去 30 日間)
Yue Zhang
Yue Zhang 2019 年 9 月 2 日
コメント済み: Yue Zhang 2019 年 9 月 2 日
I have want to generate a 5-element vector:
A = [a1, a2, a3, a4, a5]
such that each element (a1...a5) could take on one of the three values [1, 2, 3];
There should be 3^5 number of outputs.
It should be easy but i have been trying for the last 90 minutes and got no luck.
  4 件のコメント
madhan ravi
madhan ravi 2019 年 9 月 2 日
You still haven’t answered Stephen’s last question.
Yue Zhang
Yue Zhang 2019 年 9 月 2 日
No. [1, 1, 1, 1, 2] and [1, 1, 1, 1, 2] should not be two different outputs. However [1, 1, 1, 1, 2] and [1, 1, 1, 2, 1] should.

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

回答 (2 件)

Walter Roberson
Walter Roberson 2019 年 9 月 2 日
[Ac{1:5}] = ndgrid(1:3);
result = cell2mat(cellfun(@(M) M(:), Ac, 'uniform', 0));
  1 件のコメント
Yue Zhang
Yue Zhang 2019 年 9 月 2 日
Thank you! This worked wonders!

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


Andrei Bobrov
Andrei Bobrov 2019 年 9 月 2 日
out = fullfact(3*ones(1,5));

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by