Creating combinations of 3 vectors

2 ビュー (過去 30 日間)
Matt
Matt 2018 年 11 月 14 日
コメント済み: Akira Agata 2018 年 11 月 14 日
I have a 3x3 array
A = [50,20,30; 55,25,35; 60,30,40]
and want to produce the 27 combinations below. How can I do this?
Result = [50,20,30; 50,20,35; 50,20,40; 50,25,30; 50,25,35; 50,25,40; 50,30,30; 50,30,35; 50,30,40; 55,20,30; 55,20,35; 55,20,40; 55,25,30; 55,25,35; 55,25,40; 55,30,30; 55,30,35; 55,30,40; 60,20,30; 60,20,35; 60,20,40; 60,25,30; 60,25,35; 60,25,40; 60,30,30; 60,30,35; 60,30,40].
Thanks,
Matt

採用された回答

Akira Agata
Akira Agata 2018 年 11 月 14 日
One possible solution would be like this:
A = [50,20,30; 55,25,35; 60,30,40];
[p1,p2,p3] = ndgrid(1:3);
Result = [A(p3(:),1),A(p2(:),2),A(p1(:),3)];
  1 件のコメント
madhan ravi
madhan ravi 2018 年 11 月 14 日
+1 neat

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

その他の回答 (1 件)

Guillaume
Guillaume 2018 年 11 月 14 日
Akira's answer is how I'd do it. Just for the record here is another method
indices = dec2base(0:size(A, 1)^size(A, 2)-1, size(A, 1)) - '0' + 1 + (0:size(A, 2)-1)*size(A, 1);
A(indices)
Works for A up to 10 rows.
  1 件のコメント
Akira Agata
Akira Agata 2018 年 11 月 14 日
What a coincidence!
Thank you for adding another solution :-)

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

カテゴリ

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

製品


リリース

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by