I have M=2 S=2 and having 4 possible combination of these two. Need to form a matrix from these four possibilities a= [1 1 2 2;1 2 1 2]. so i want to make a generic code using for loop that may be valid for every value of M and S....Thanks

2 ビュー (過去 30 日間)
M=2; S=[1,2];pos=size(S,2)^M;for i=1:M for j=1:pos X(i,j)=S(i) end end
This is not generic code...secondly the result i want is not perfect for the scenario i attached a pic of the scenario but need a generic code that will be valid for increasing M and s
  2 件のコメント
KL
KL 2017 年 9 月 1 日
It's not clear. How does your desired output look like?
Muhammad Haris
Muhammad Haris 2017 年 9 月 4 日
Desired output should be 2*4 matrix for the above scenario.
i attached a pic of the results

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

採用された回答

José-Luis
José-Luis 2017 年 9 月 1 日
doc perms

その他の回答 (1 件)

KL
KL 2017 年 9 月 4 日
M = 2;
S = [1,2];
S1 = reshape(repmat(S,size(S,2),1),1,size(S,2)*M);
S2 = repmat(S,1,size(S,2));
X = [S1; S2];

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by