Matrix with full combinations of vectors'/matrices' elements

1 回表示 (過去 30 日間)
Luca Freilino
Luca Freilino 2019 年 4 月 13 日
編集済み: Stephen23 2019 年 4 月 13 日
Hi everyone. I have a problem. I have 2 vectors and 2 matrices, that come from the nchoosek function. I'd like to know if there is a way to find a final matrix with all the possible combinations of their elements. E.g. A = [1 2 3] B = [4 5; 6 7] C = [8 9; 10 11] D = [12 13]. The final matrix should be: [1 4 5 8 9 12; 1 4 5 8 9 13; 1 4 5 10 11 12; ............ 2 6 7 10 11 12; 2 6 7 10 11 13; ............ Etc]
I have already tried with some for cycle (and it works), but I would like to find a faster and better solution. Thank you in advance, Luca

回答 (1 件)

Stephen23
Stephen23 2019 年 4 月 13 日
編集済み: Stephen23 2019 年 4 月 13 日
>> A = [1;2;3]; % Column vector!
>> B = [4,5;6,7];
>> C = [8,9;10,11];
>> D = [12;13]; % Column vector!
>> An = size(A,1);
>> Bn = size(B,1);
>> Cn = size(C,1);
>> Dn = size(D,1);
>> [Dx,Cx,Bx,Ax] = ndgrid(1:Dn,1:Cn,1:Bn,1:An);
>> Z = [A(Ax(:),:),B(Bx(:),:),C(Cx(:),:),D(Dx(:),:)];
Z =
1 4 5 8 9 12
1 4 5 8 9 13
1 4 5 10 11 12
1 4 5 10 11 13
1 6 7 8 9 12
1 6 7 8 9 13
... lots of rows here
3 4 5 10 11 12
3 4 5 10 11 13
3 6 7 8 9 12
3 6 7 8 9 13
3 6 7 10 11 12
3 6 7 10 11 13

カテゴリ

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

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by