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 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