Matrix with full combinations of vectors'/matrices' elements

1 Ansicht (letzte 30 Tage)
Luca Freilino
Luca Freilino am 13 Apr. 2019
Bearbeitet: Stephen23 am 13 Apr. 2019
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

Antworten (1)

Stephen23
Stephen23 am 13 Apr. 2019
Bearbeitet: Stephen23 am 13 Apr. 2019
>> 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

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Produkte


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by