How to combine two or more than two matrix

1 回表示 (過去 30 日間)
Asad Abbas
Asad Abbas 2016 年 9 月 7 日
コメント済み: Stephen23 2021 年 12 月 21 日
Please help me. Here is Example
a=[1 0 1, 1 1 0, 1 1 1] and b=[0 1, 1 0 ]
I want combine these two matrix with each possible combination such as
1 0 1 01
1 0 1 10
1 1 0 01
1 1 0 10
1 1 1 01
1 1 1 10
Total possible solutions are 3(rows of a)*2(rows of b)=6
I have A=119*9 and B=75*12

採用された回答

Stephen23
Stephen23 2016 年 9 月 7 日
編集済み: Stephen23 2016 年 9 月 7 日
you could use Jos' excellent FEX submssion allcomb:
>> a = [1,0,1;1,1,0;1,1,1];
>> b = [0,1;1,0];
>> cell2mat(allcomb(num2cell(a,2),num2cell(b,2)))
ans =
1 0 1 0 1
1 0 1 1 0
1 1 0 0 1
1 1 0 1 0
1 1 1 0 1
1 1 1 1 0
There is also the special case where the number rows are not multiples of each other:
>> [repmat(a,size(b,1),1),repmat(b,size(a,1),1)]
ans =
1 0 1 0 1
1 1 0 1 0
1 1 1 0 1
1 0 1 1 0
1 1 0 0 1
1 1 1 1 0
  3 件のコメント
Christos Traianos
Christos Traianos 2021 年 12 月 21 日
Hi! Thanks for the answer! How [repmat(a,size(b,1),1),repmat(b,size(a,1),1)] could be used for n number of double arrays?
Thanks in advance!
Stephen23
Stephen23 2021 年 12 月 21 日
@Christos Traianos: using ALLCOMB is probably the simplest approach:
C = {[1,0,1;1,1,0;1,1,1],[0,1;1,0]}; % any number of matrices
D = cellfun(@(m)num2cell(m,2),C,'uni',0);
M = cell2mat(allcomb(D{:}))
M = 6×5
1 0 1 0 1 1 0 1 1 0 1 1 0 0 1 1 1 0 1 0 1 1 1 0 1 1 1 1 1 0

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by