Matrix with full combinations of vectors'/matrices' elements

1 vue (au cours des 30 derniers jours)
Luca Freilino
Luca Freilino le 13 Avr 2019
Modifié(e) : Stephen23 le 13 Avr 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

Réponses (1)

Stephen23
Stephen23 le 13 Avr 2019
Modifié(e) : Stephen23 le 13 Avr 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

Catégories

En savoir plus sur Matrices and Arrays dans Help Center et File Exchange

Produits


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by