I WANT TO CREATE MATRIX COMBINATION

2 ビュー (過去 30 日間)
ARBAZKHAN PATHAN
ARBAZKHAN PATHAN 2023 年 1 月 6 日
コメント済み: ARBAZKHAN PATHAN 2023 年 1 月 6 日
e.g. A = [1 2 3] B = [4,5,6]
I want the new matrix c to be
C = [1 3; 1 5; 1 6; 2 4; 2 5; 2 6; 3 4; 3 5; 3 6]

採用された回答

Karim
Karim 2023 年 1 月 6 日
% setup the original data, note the shape!
A = [1;2;3];
B = [4;5;6];
% create the combinations
C = [ repmat(A,numel(B),1) reshape( repmat(B',numel(A),1),[],1) ]
C = 9×2
1 4 2 4 3 4 1 5 2 5 3 5 1 6 2 6 3 6
  1 件のコメント
ARBAZKHAN PATHAN
ARBAZKHAN PATHAN 2023 年 1 月 6 日
Thank you Sir!!

サインインしてコメントする。

その他の回答 (1 件)

Dyuman Joshi
Dyuman Joshi 2023 年 1 月 6 日
A = [1 2 3];
B = [4 5 6];
nA=numel(A);
nB=numel(B);
%If you have Stats and ML Toolbox
y=fullfact([nA nB]);
C=[A(y(:,2));B(y(:,1))]'
C = 9×2
1 4 1 5 1 6 2 4 2 5 2 6 3 4 3 5 3 6
%otherwise
z=dec2base(0:nA*nB-1,max(nA,nB))-47;
D=[A(z(:,1))' B(z(:,2))']
D = 9×2
1 4 1 5 1 6 2 4 2 5 2 6 3 4 3 5 3 6
%another method using ndgrid
n=2; %two inputs A and B
[E{flip(1:n)}]=ndgrid(B,A);
reshape(cat(n,E{:}),'',n)
ans = 9×2
1 4 1 5 1 6 2 4 2 5 2 6 3 4 3 5 3 6
  1 件のコメント
ARBAZKHAN PATHAN
ARBAZKHAN PATHAN 2023 年 1 月 6 日
THANK YOU SIR!!!

サインインしてコメントする。

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by