Matrix with full combinations of vectors'/matrices' elements

1 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by